Source code: fzi/injectj/config/ConfigDialog.java
1 // This file is part of the Inject/J project
2 // (C) 1999-2001 Forschungszentrum Informatik (FZI) Karlsruhe
3 // Please visit our website at http://injectj.fzi.de
4
5 package fzi.injectj.config;
6
7 import java.awt.*;
8 import javax.swing.*;
9 import java.awt.event.*;
10 import java.util.*;
11 import java.lang.reflect.Method;
12 import java.io.File;
13 import fzi.injectj.language.*;
14 import fzi.injectj.Main;
15
16 /** Graphical interface for manipulating the Inject/J configuration
17 * file and the currently used global settings.
18 *
19 * @author Volker Kuttruff
20 */
21 public class ConfigDialog extends JDialog
22 {
23 static protected Hashtable languages = null;
24
25 JPanel panel = new JPanel();
26 JComboBox lookAndFeelBox = null;
27 JLabel lookAndFeelLabel = new JLabel();
28 JComboBox languageBox = null;
29 JLabel languageLabel = new JLabel();
30 JButton okButton = new JButton();
31 JButton cancelButton = new JButton();
32 JButton applyButton = new JButton();
33 JButton saveButton = new JButton();
34
35 CodeMapper lastCodeMapper = null;
36 LookAndFeel lastLookAndFeel = null;
37 boolean languageChanged = false;
38 boolean lookAndFeelChanged = false;
39
40
41 MainWindow mainWindow = null;
42 JLabel projectBaseDirLabel = new JLabel();
43 JTextField projectBaseField = new JTextField();
44 JLabel scriptBaseDirLabel = new JLabel();
45 JButton chooseProjectBaseButton = new JButton();
46 JTextField scriptBaseField = new JTextField();
47 JButton chooseScriptBaseDirButton = new JButton();
48 JLabel javaBaseDirLabel = new JLabel();
49 JTextField javaBaseField = new JTextField();
50 JButton chooseJavaBaseDirButton = new JButton();
51 JLabel libraryBaseDirLabel = new JLabel();
52 JTextField libraryBaseField = new JTextField();
53 JButton chooseLibraryBaseDirButton = new JButton();
54 JCheckBox ignoreCaseCheckBox = new JCheckBox();
55
56 public ConfigDialog(JFrame owner, String title, boolean modal)
57 {
58 super(owner, title, modal);
59 if (owner instanceof MainWindow) mainWindow = (MainWindow) owner;
60 try
61 {
62 jbInit();
63 pack();
64 }
65 catch(Exception ex)
66 {
67 ex.printStackTrace();
68 }
69 }
70
71 public ConfigDialog()
72 {
73 this(null, "", false);
74 }
75
76 public static void main(String[] args) {
77 ConfigDialog configDialog = new ConfigDialog(null,"Inject/J Settings",true);
78 configDialog.setVisible(true);
79 }
80
81 public void setVisible(boolean isVisible) {
82 lastLookAndFeel = UIManager.getLookAndFeel();
83 lastCodeMapper = CodeMapper.getCurrentCodeMapper();
84 if (lastLookAndFeel!=null) {
85 String lastLookAndFeelName = lastLookAndFeel.getName();
86 lookAndFeelBox.setSelectedItem(lastLookAndFeelName);
87 }
88 if (lastCodeMapper!=null) {
89 String className = lastCodeMapper.getClass().getName();
90 String lastLanguageName = getSimpleClassName(className);
91 languageBox.setSelectedItem(lastLanguageName);
92 }
93 super.setVisible(isVisible);
94 }
95
96 void jbInit() throws Exception
97 {
98 initComboBoxes();
99 this.setSize(new Dimension(400, 400));
100 panel.setLayout(null);
101 panel.setPreferredSize(new Dimension(400, 400));
102 lookAndFeelBox.setBounds(new Rectangle(160, 25, 120, 25));
103 lookAndFeelLabel.setHorizontalAlignment(SwingConstants.RIGHT);
104 lookAndFeelLabel.setText("Look And Feel");
105 lookAndFeelLabel.setBounds(new Rectangle(5, 25, 150, 25));
106 languageBox.setBounds(new Rectangle(160, 65, 120, 25));
107 languageLabel.setHorizontalAlignment(SwingConstants.RIGHT);
108 languageLabel.setText("Language");
109 languageLabel.setBounds(new Rectangle(5, 65, 150, 25));
110 okButton.setText("OK");
111 okButton.setBounds(new Rectangle(15, 360, 110, 30));
112 okButton.addActionListener(new java.awt.event.ActionListener()
113 {
114
115 public void actionPerformed(ActionEvent e)
116 {
117 okButton_actionPerformed(e);
118 }
119 });
120 cancelButton.setText("Cancel");
121 cancelButton.setBounds(new Rectangle(145, 360, 110, 30));
122 cancelButton.addActionListener(new java.awt.event.ActionListener()
123 {
124
125 public void actionPerformed(ActionEvent e)
126 {
127 cancelButton_actionPerformed(e);
128 }
129 });
130 applyButton.setText("Apply");
131 applyButton.setBounds(new Rectangle(275, 360, 110, 30));
132 applyButton.addActionListener(new java.awt.event.ActionListener()
133 {
134
135 public void actionPerformed(ActionEvent e)
136 {
137 applyButton_actionPerformed(e);
138 }
139 });
140 saveButton.setText("Save");
141 saveButton.setBounds(new Rectangle(275, 320, 110, 30));
142 saveButton.addActionListener(new java.awt.event.ActionListener()
143 {
144
145 public void actionPerformed(ActionEvent e)
146 {
147 saveButton_actionPerformed(e);
148 }
149 });
150
151 projectBaseDirLabel.setHorizontalAlignment(SwingConstants.RIGHT);
152 projectBaseDirLabel.setText("Project Base Directory");
153 projectBaseDirLabel.setBounds(new Rectangle(5, 105, 150, 25));
154 projectBaseField.setBounds(new Rectangle(160, 105, 195, 25));
155 String projectBaseDir = GlobalSettings.projectBaseDir;
156 if (projectBaseDir!=null && !(projectBaseDir.equals(""))) {
157 projectBaseField.setText(projectBaseDir);
158 }
159 chooseProjectBaseButton.setText("...");
160 chooseProjectBaseButton.setBounds(new Rectangle(360, 105, 25, 25));
161 chooseProjectBaseButton.addActionListener(new java.awt.event.ActionListener()
162 {
163
164 public void actionPerformed(ActionEvent e)
165 {
166 chooseProjectBaseButton_actionPerformed(e);
167 }
168 });
169
170 scriptBaseDirLabel.setHorizontalAlignment(SwingConstants.RIGHT);
171 scriptBaseDirLabel.setText("Script Base Directory");
172 scriptBaseDirLabel.setBounds(new Rectangle(5, 145, 150, 25));
173 scriptBaseField.setBounds(new Rectangle(160, 145, 195, 25));
174 String scriptBaseDir = GlobalSettings.scriptBaseDir;
175 if (scriptBaseDir!=null && !(scriptBaseDir.equals(""))) {
176 scriptBaseField.setText(scriptBaseDir);
177 }
178 chooseScriptBaseDirButton.setText("...");
179 chooseScriptBaseDirButton.setBounds(new Rectangle(360, 145, 25, 25));
180 chooseScriptBaseDirButton.addActionListener(new java.awt.event.ActionListener()
181 {
182
183 public void actionPerformed(ActionEvent e)
184 {
185 chooseScriptBaseDirButton_actionPerformed(e);
186 }
187 });
188
189 javaBaseDirLabel.setHorizontalAlignment(SwingConstants.RIGHT);
190 javaBaseDirLabel.setText("Java Base Directory");
191 javaBaseDirLabel.setBounds(new Rectangle(5, 185, 150, 25));
192 javaBaseField.setBounds(new Rectangle(160, 185, 195, 25));
193 String javaBaseDir = GlobalSettings.javaBaseDir;
194 if (javaBaseDir!=null && !(javaBaseDir.equals(""))) {
195 javaBaseField.setText(javaBaseDir);
196 }
197 chooseJavaBaseDirButton.setText("...");
198 chooseJavaBaseDirButton.setBounds(new Rectangle(360, 185, 25, 25));
199 chooseJavaBaseDirButton.addActionListener(new java.awt.event.ActionListener()
200 {
201
202 public void actionPerformed(ActionEvent e)
203 {
204 chooseJavaBaseDirButton_actionPerformed(e);
205 }
206 });
207
208 libraryBaseDirLabel.setHorizontalAlignment(SwingConstants.RIGHT);
209 libraryBaseDirLabel.setText("Libraries Base Directory");
210 libraryBaseDirLabel.setBounds(new Rectangle(5, 225, 150, 25));
211 libraryBaseField.setBounds(new Rectangle(160, 225, 195, 25));
212 String libraryBaseDir = GlobalSettings.libraryPath;
213 if (libraryBaseDir!=null && !(libraryBaseDir.equals(""))) {
214 libraryBaseField.setText(libraryBaseDir);
215 }
216 chooseLibraryBaseDirButton.setText("...");
217 chooseLibraryBaseDirButton.setBounds(new Rectangle(360, 225, 25, 25));
218 chooseLibraryBaseDirButton.addActionListener(new java.awt.event.ActionListener()
219 {
220
221 public void actionPerformed(ActionEvent e)
222 {
223 chooseLibraryBaseDirButton_actionPerformed(e);
224 }
225 });
226
227
228 ignoreCaseCheckBox.setText("Ignore upper/lower case in path specifiers");
229 ignoreCaseCheckBox.setBounds(new Rectangle(15, 280, 370, 25));
230 ignoreCaseCheckBox.setSelected(GlobalSettings.ignoreCase);
231
232 getContentPane().add(panel);
233 panel.add(lookAndFeelLabel, null);
234 panel.add(lookAndFeelBox, null);
235 panel.add(languageBox, null);
236 panel.add(languageLabel, null);
237 panel.add(okButton, null);
238 panel.add(cancelButton, null);
239 panel.add(applyButton, null);
240 panel.add(saveButton, null);
241 panel.add(projectBaseDirLabel, null);
242 panel.add(projectBaseField, null);
243 panel.add(scriptBaseDirLabel, null);
244 panel.add(chooseProjectBaseButton, null);
245 panel.add(scriptBaseField, null);
246 panel.add(chooseScriptBaseDirButton, null);
247 panel.add(javaBaseDirLabel, null);
248 panel.add(javaBaseField, null);
249 panel.add(chooseJavaBaseDirButton, null);
250 panel.add(libraryBaseDirLabel,null);
251 panel.add(libraryBaseField,null);
252 panel.add(chooseLibraryBaseDirButton, null);
253 panel.add(ignoreCaseCheckBox, null);
254
255 //Center window
256 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
257 Dimension frameSize = this.getSize();
258 if (frameSize.height > screenSize.height)
259 frameSize.height = screenSize.height;
260 if (frameSize.width > screenSize.width)
261 frameSize.width = screenSize.width;
262 this.setLocation((screenSize.width- frameSize.width) / 2, (screenSize.height - frameSize.height) /3);
263 this.setResizable(false);
264 updateLabels();
265 }
266
267 protected void initComboBoxes() {
268 if (languages!=null) languages.clear();
269 Vector resultingLanguages = new Vector(GlobalSettings.languageClassNames.length);
270 Vector resultingLookAndFeelNames = new Vector();
271
272 ClassLoader loader = ClassLoader.getSystemClassLoader();
273
274 if (GlobalSettings.languageClassNames!=null) {
275 for (int i=0; i<GlobalSettings.languageClassNames.length; i++) {
276 try {
277 Class languageClass = loader.loadClass(GlobalSettings.languageClassNames[i]);
278 String simpleName = getSimpleClassName(GlobalSettings.languageClassNames[i]);
279 resultingLanguages.addElement(simpleName);
280 if (languages==null) languages = new Hashtable();
281 languages.put(simpleName, GlobalSettings.languageClassNames[i]);
282 }
283 catch (Exception e) {}
284 }
285 }
286
287 UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
288 for (int i=0; i<infos.length; i++) {
289 UIManager.LookAndFeelInfo info = infos[i];
290 resultingLookAndFeelNames.addElement(info.getName());
291 }
292
293 lookAndFeelBox = new JComboBox(resultingLookAndFeelNames);
294 languageBox = new JComboBox(resultingLanguages);
295 }
296
297 public static Class getLookAndFeelClass(String lookAndFeelName) {
298
299 UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
300 String className = null;
301 for (int i=0; i<infos.length; i++) {
302 UIManager.LookAndFeelInfo info = infos[i];
303 if (info.getName().equals(lookAndFeelName)) {
304 className = info.getClassName();
305 }
306 }
307
308 if (className==null) return null;
309
310 ClassLoader loader = ClassLoader.getSystemClassLoader();
311
312 try {
313 Class lookAndFeelClass = loader.loadClass(className);
314 return lookAndFeelClass;
315 }
316 catch (Exception e) {}
317 return null;
318 }
319
320 public static Class getLanguageClass(String className) {
321
322 ClassLoader loader = ClassLoader.getSystemClassLoader();
323
324 try {
325 Class languageClass = loader.loadClass(className);
326 return languageClass;
327 }
328 catch (Exception e) {}
329 return null;
330 }
331
332 void applyButton_actionPerformed(ActionEvent e)
333 {
334 Class lookAndFeelClass = getLookAndFeelClass((String)lookAndFeelBox.getSelectedItem());
335 if (lookAndFeelClass!=null) {
336 try {
337 LookAndFeel newLookAndFeel = (LookAndFeel) lookAndFeelClass.newInstance();
338 UIManager.setLookAndFeel(newLookAndFeel);
339 updateComponentUI(panel);
340 }
341 catch (Exception ex) {}
342 }
343
344 if (languages!=null) {
345 String languageClassName = (String) languages.get((String)languageBox.getSelectedItem());
346 Class codeMapperClass = getLanguageClass(languageClassName);
347 if (codeMapperClass!=null) {
348 try {
349 Method setLanguageMethod = codeMapperClass.getMethod("setAsCurrentLanguage",null);
350 Method getSingleInstanceMethod = codeMapperClass.getMethod("getSingleInstance",null);
351 setLanguageMethod.invoke(getSingleInstanceMethod.invoke(null,null),null);
352 //codeMapperClass.newInstance();
353 updateLabels();
354 }
355 catch (Exception ex) {
356 }
357 }
358 }
359
360 String projectBaseDir = projectBaseField.getText();
361 if (projectBaseDir!=null && !(projectBaseDir.equals(""))) {
362 if(!(projectBaseDir.endsWith(File.separator)))
363 projectBaseDir = projectBaseDir + File.separator;
364 GlobalSettings.projectBaseDir = projectBaseDir;
365 }
366 else {
367 GlobalSettings.projectBaseDir = null;
368 }
369
370 String scriptBaseDir = scriptBaseField.getText();
371 if (scriptBaseDir!=null && !(scriptBaseDir.equals(""))) {
372 if(!(scriptBaseDir.endsWith(File.separator)))
373 scriptBaseDir = scriptBaseDir + File.separator;
374 GlobalSettings.scriptBaseDir = scriptBaseDir;
375 }
376 else {
377 GlobalSettings.scriptBaseDir = null;
378 }
379
380 String javaBaseDir = javaBaseField.getText();
381 if (javaBaseDir!=null && !(javaBaseDir.equals(""))) {
382 if(!(javaBaseDir.endsWith(File.separator)))
383 javaBaseDir = javaBaseDir + File.separator;
384 GlobalSettings.javaBaseDir = javaBaseDir;
385 }
386 else {
387 GlobalSettings.javaBaseDir = null;
388 }
389
390 String libraryBaseDir = libraryBaseField.getText();
391 if (libraryBaseDir!=null && !(libraryBaseDir.equals(""))) {
392 GlobalSettings.libraryPath = libraryBaseDir;
393 }
394 else {
395 GlobalSettings.libraryPath = null;
396 }
397
398
399 GlobalSettings.ignoreCase = ignoreCaseCheckBox.isSelected();
400
401 if (mainWindow!=null) {
402 mainWindow.updateAll();
403 mainWindow.setWindowTitle();
404 }
405 }
406
407 void okButton_actionPerformed(ActionEvent e)
408 {
409 applyButton_actionPerformed(null);
410 this.dispose();
411 }
412
413 void cancelButton_actionPerformed(ActionEvent e)
414 {
415 try {
416 lastCodeMapper.setAsCurrentLanguage();
417 UIManager.setLookAndFeel(lastLookAndFeel);
418 this.dispose();
419 }
420 catch (Exception ex) {}
421 }
422
423 protected void updateComponentUI(JComponent component) {
424 component.updateUI();
425 Component[] components = component.getComponents();
426 for (int i=0; i<components.length; i++) {
427 if(components[i] instanceof JComponent) {
428 updateComponentUI((JComponent) components[i]);
429 }
430 }
431 }
432
433 protected void updateLabels() {
434 this.setTitle(CodeMapper.getText(LabelCode.CONFIGDIALOG_TITLE));
435 applyButton.setText(CodeMapper.getText(LabelCode.APPLY_BUTTON));
436 cancelButton.setText(CodeMapper.getText(LabelCode.CANCEL_BUTTON));
437 okButton.setText(CodeMapper.getText(LabelCode.OK_BUTTON));
438 saveButton.setText(CodeMapper.getText(LabelCode.SAVE_BUTTON));
439 languageLabel.setText(CodeMapper.getText(LabelCode.LANGUAGE_LABEL));
440 lookAndFeelLabel.setText(CodeMapper.getText(LabelCode.LOOKANDFEEL_LABEL));
441 projectBaseDirLabel.setText(CodeMapper.getText(LabelCode.PROJECTBASE_LABEL));
442 scriptBaseDirLabel.setText(CodeMapper.getText(LabelCode.SCRIPTBASE_LABEL));
443 javaBaseDirLabel.setText(CodeMapper.getText(LabelCode.JAVABASE_LABEL));
444 libraryBaseDirLabel.setText(CodeMapper.getText(LabelCode.LIBRARYBASE_LABEL));
445 ignoreCaseCheckBox.setText(CodeMapper.getText(LabelCode.IGNORECASECHECK_LABEL));
446 }
447
448 void saveButton_actionPerformed(ActionEvent e)
449 {
450 applyButton_actionPerformed(null);
451 Main.writeGlobalSettings();
452 }
453
454 void chooseProjectBaseButton_actionPerformed(ActionEvent e)
455 {
456 String windowTitle = CodeMapper.getText(LabelCode.PROJECTBASE_LABEL);
457 String newProjectBase = getBaseDir(windowTitle, GlobalSettings.projectBaseDir);
458 if (newProjectBase!=null) {
459 projectBaseField.setText(newProjectBase);
460 }
461 }
462
463 void chooseScriptBaseDirButton_actionPerformed(ActionEvent e)
464 {
465 String windowTitle = CodeMapper.getText(LabelCode.SCRIPTBASE_LABEL);
466 String newScriptBase = getBaseDir(windowTitle, GlobalSettings.scriptBaseDir);
467 if (newScriptBase!=null) {
468 scriptBaseField.setText(newScriptBase);
469 }
470 }
471
472 void chooseJavaBaseDirButton_actionPerformed(ActionEvent e)
473 {
474 String windowTitle = CodeMapper.getText(LabelCode.JAVABASE_LABEL);
475 String newJavaBase = getBaseDir(windowTitle, GlobalSettings.javaBaseDir);
476 if (newJavaBase!=null) {
477 javaBaseField.setText(newJavaBase);
478 }
479 }
480
481 void chooseLibraryBaseDirButton_actionPerformed(ActionEvent e)
482 {
483 String windowTitle = CodeMapper.getText(LabelCode.LIBRARYBASE_LABEL);
484 String lastLibraryDir = null;
485 if (GlobalSettings.libraryPath!=null) {
486 int index = GlobalSettings.libraryPath.lastIndexOf(File.pathSeparator);
487 if (index==-1) lastLibraryDir = GlobalSettings.libraryPath;
488 else lastLibraryDir = GlobalSettings.libraryPath.substring(index+1);
489 }
490 String newLibraryBase = getBaseDir(windowTitle, lastLibraryDir);
491 if (newLibraryBase!=null) {
492 if (GlobalSettings.libraryPath!=null && !GlobalSettings.libraryPath.equals("")) {
493
494 BoolDialog boolDlg = new BoolDialog(this,CodeMapper.getText(LabelCode.ADD_REPLACE_DIR_TITLE), true);
495 boolDlg.setMessage(CodeMapper.getText(LabelCode.ADD_REPLACE_LABEL));
496 boolDlg.setTrueButtonLabel(CodeMapper.getText(LabelCode.ADD_TO_PATH_LABEL));
497 boolDlg.setFalseButtonLabel(CodeMapper.getText(LabelCode.REPLACE_PATH_LABEL));
498 boolDlg.setVisible(true);
499
500 if (boolDlg.getResult()) {
501 if (GlobalSettings.libraryPath!=null) {
502 if (GlobalSettings.libraryPath.endsWith(File.pathSeparator)) {
503 newLibraryBase = GlobalSettings.libraryPath + newLibraryBase;
504 }
505 else {
506 newLibraryBase = GlobalSettings.libraryPath + File.pathSeparator + newLibraryBase;
507 }
508 }
509 }
510 }
511 newLibraryBase = newLibraryBase.replace('\\','/');
512 libraryBaseField.setText(newLibraryBase);
513 }
514 }
515
516 String getBaseDir(String dialogTitle, String oldBaseDir) {
517 JFileChooser dirChooser;
518 if (oldBaseDir!=null) {
519 dirChooser = new JFileChooser(oldBaseDir);
520 File oldDir = new File(oldBaseDir);
521 dirChooser.setSelectedFile(oldDir);
522 }
523 else dirChooser = new JFileChooser();
524
525 dirChooser.setMultiSelectionEnabled(false);
526 dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
527 dirChooser.setDialogTitle(dialogTitle);
528
529 int result = dirChooser.showOpenDialog(this);
530 if (result!=JFileChooser.APPROVE_OPTION) return null;
531 File newDir = dirChooser.getSelectedFile();
532 if (!(newDir.isDirectory())) return null;
533 return newDir.getPath();
534 }
535
536 private String getSimpleClassName(String className) {
537
538 int index = className.lastIndexOf(".");
539 String simpleName;
540 if (index!=-1) {
541 try {
542 simpleName = className.substring(index+1);
543 }
544 catch (IndexOutOfBoundsException e) {
545 simpleName = className;
546 }
547 }
548 else {
549 simpleName = className;
550 }
551 return simpleName;
552 }
553 }