View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.dialogs;
13  
14  import org.eclipse.jface.dialogs.Dialog;
15  import org.eclipse.jface.dialogs.IDialogConstants;
16  import org.eclipse.swt.SWT;
17  import org.eclipse.swt.events.ModifyEvent;
18  import org.eclipse.swt.events.ModifyListener;
19  import org.eclipse.swt.layout.GridData;
20  import org.eclipse.swt.widgets.Composite;
21  import org.eclipse.swt.widgets.Control;
22  import org.eclipse.swt.widgets.Label;
23  import org.eclipse.swt.widgets.Shell;
24  import org.eclipse.swt.widgets.Text;
25  
26  /***
27   * Dialog to insert or modify processing instruction.
28   * 
29   * @author Tomáš Studva 22.8.2006
30   */
31  public class ProcessingInstructionDialog extends Dialog {
32          /***
33           * The title of the dialog.
34           */
35          private String title;
36  
37          /***
38           * Target of processing instruction.
39           */
40          private String target;
41  
42          /***
43           * Data of processing instruction.
44           */
45          private String data;
46  
47          /***
48           * Input text widget for target.
49           */
50          private Text targetText;
51  
52          /***
53           * Input text widget for data.
54           */
55          private Text dataText;
56  
57          /***
58           * Error message label widget.
59           */
60          private Text errorMessageText;
61  
62          /***
63           * Preview label widget.
64           */
65          private Text preview;
66  
67          /***
68           * Creates dialog to insert or modify processing instruction.
69           * 
70           * @param parentShell
71           *                the parent shell, or <code>null</code> to create a
72           *                top-level shell
73           * @param dialogTitle
74           *                the dialog title, or <code>null</code> if none
75           * @param initialTarget
76           *                initial target of processing instruction
77           * @param initialData
78           *                initial data of processing instruction
79           */
80          public ProcessingInstructionDialog(Shell parentShell,
81                          String dialogTitle, String initialTarget,
82                          String initialData) {
83                  super(parentShell);
84                  this.title = dialogTitle;
85                  this.target = initialTarget;
86                  this.data = initialData;
87          }
88  
89          /*
90           * (non-Javadoc)
91           * 
92           * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
93           */
94          protected void configureShell(Shell shell) {
95                  super.configureShell(shell);
96                  if (title != null) {
97                          shell.setText(title);
98                  }
99          }
100 
101         /*
102          * (non-Javadoc) Method declared on Dialog.
103          */
104         protected void buttonPressed(int buttonId) {
105                 if (buttonId == IDialogConstants.OK_ID) {
106                         this.target = targetText.getText();
107                         this.data = dataText.getText();
108                 } else {
109                         this.target = null;
110                         this.data = null;
111                 }
112                 super.buttonPressed(buttonId);
113         }
114 
115         /*
116          * (non-Javadoc)
117          * 
118          * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
119          */
120         protected void createButtonsForButtonBar(Composite parent) {
121                 // create OK and Cancel buttons by default
122                 createButton(parent, IDialogConstants.OK_ID,
123                                 IDialogConstants.OK_LABEL, true);
124                 createButton(parent, IDialogConstants.CANCEL_ID,
125                                 IDialogConstants.CANCEL_LABEL, false);
126                 // do this here because setting the text will set enablement on
127                 // the ok
128                 // button
129                 this.targetText.setFocus();
130                 if (this.target != null) {
131                         this.targetText.setText(this.target);
132                         this.targetText.selectAll();
133                 }
134                 if (this.data != null) {
135                         this.dataText.setText(this.data);
136                         this.dataText.selectAll();
137                 }
138         }
139 
140         @Override
141         protected Control createDialogArea(Composite parent) {
142                 // create composite
143                 Composite composite = (Composite) super
144                                 .createDialogArea(parent);
145                 // preview
146                 this.preview = new Text(composite, SWT.READ_ONLY);
147                 this.preview
148                                 .setLayoutData(new GridData(
149                                                 GridData.GRAB_HORIZONTAL
150                                                                 | GridData.HORIZONTAL_ALIGN_FILL));
151                 this.preview.setBackground(this.preview.getDisplay()
152                                 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
153 
154                 // create Target label
155                 Label label = new Label(composite, SWT.WRAP);
156                 label.setText("Target of instruction:");
157                 GridData grid = new GridData(SWT.BEGINNING, SWT.BEGINNING,
158                                 false, false);
159                 grid.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
160                 label.setLayoutData(grid);
161                 label.setFont(parent.getFont());
162                 // create target input field
163                 this.targetText = new Text(composite, SWT.SINGLE | SWT.BORDER);
164                 this.targetText
165                                 .setLayoutData(new GridData(
166                                                 GridData.GRAB_HORIZONTAL
167                                                                 | GridData.HORIZONTAL_ALIGN_FILL));
168                 this.targetText.addModifyListener(new ModifyListener() {
169                         public void modifyText(ModifyEvent e) {
170                                 validateInput();
171                         }
172                 });
173 
174                 // create data label
175                 label = new Label(composite, SWT.WRAP);
176                 label.setText("Data of instruction:");
177                 grid = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
178                 grid.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
179                 label.setLayoutData(grid);
180                 label.setFont(parent.getFont());
181                 // create data input field
182                 this.dataText = new Text(composite, SWT.SINGLE | SWT.BORDER);
183                 this.dataText
184                                 .setLayoutData(new GridData(
185                                                 GridData.GRAB_HORIZONTAL
186                                                                 | GridData.HORIZONTAL_ALIGN_FILL));
187                 this.dataText.addModifyListener(new ModifyListener() {
188                         public void modifyText(ModifyEvent e) {
189                                 validateInput();
190                         }
191                 });
192 
193                 this.errorMessageText = new Text(composite, SWT.READ_ONLY);
194                 this.errorMessageText
195                                 .setLayoutData(new GridData(
196                                                 GridData.GRAB_HORIZONTAL
197                                                                 | GridData.HORIZONTAL_ALIGN_FILL));
198                 this.errorMessageText.setBackground(this.errorMessageText
199                                 .getDisplay().getSystemColor(
200                                                 SWT.COLOR_WIDGET_BACKGROUND));
201                 setErrorMessage("");
202 
203                 applyDialogFont(composite);
204                 return composite;
205         }
206 
207         /***
208          * Getter.
209          * 
210          * @return target of processing instruction
211          */
212         public String getTarget() {
213                 return this.target;
214         }
215 
216         /***
217          * Getter.
218          * 
219          * @return data of processing instruction
220          */
221         public String getData() {
222                 return this.data;
223         }
224 
225         /***
226          * Validates the input. Restriction is on target, it can be gene-ref
227          * target.
228          */
229         protected void validateInput() {
230                 String errorMessage = "";
231                 String t = this.targetText.getText();
232                 String d = this.dataText.getText();
233                 if (t.equals("gene-ref"))
234                         errorMessage = "Illegal target. This target is reserved in EuroMath for GENE."; //$NON-NLS-N$
235                 if (t.equals(""))
236                         errorMessage = "Illegal target."; //$NON-NLS-N$
237                 setErrorMessage(errorMessage);
238                 // update preview
239                 this.preview.setText("<?" + t + " " + d + "?>");//$NON-NLS-N$ //$NON-NLS-N$ //$NON-NLS-N$
240                 this.preview.getParent().update();
241         }
242 
243         /***
244          * Sets or clears the error message. If not <code>""</code>, the OK
245          * button is disabled.
246          * 
247          * @param errorMessage
248          *                the error message
249          * @since 3.0
250          */
251         public void setErrorMessage(String errorMessage) {
252                 assert (errorMessage != null);
253                 if (this.errorMessageText != null
254                                 && !this.errorMessageText.isDisposed()) {
255                         this.errorMessageText.setText(errorMessage);
256                         this.errorMessageText.getParent().update();
257                         // Access the ok button by id, in case clients have
258                         // overridden button creation.
259                         // See
260                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
261                         Control button = getButton(IDialogConstants.OK_ID);
262                         if (button != null) {
263                                 button.setEnabled(errorMessage == "");
264                         }
265                 }
266         }
267 }