Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: fzi/injectj/config/panel/JavaSourcePathsPanel.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.panel;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   import javax.swing.*;
10  import java.io.*;
11  import java.util.*;
12  import fzi.injectj.config.*;
13  import fzi.injectj.language.*;
14  import fzi.injectj.util.*;
15  import fzi.injectj.access.MOPAccess;
16  import fzi.injectj.*;
17  
18  /** Panel to select source paths in which Java sources are accepted.
19    * A filename filter can be generated from this informations.
20    * If the user hits the "Next"-button, the underlying MOP is asked
21    * to parse the Java sources.
22    *
23    * @author Volker Kuttruff
24    */
25  
26  public class JavaSourcePathsPanel extends JPanel implements InjectJPanel
27  {
28    MainWindow mainWindow;
29  
30    JButton nextButton = new JButton();
31    JButton helpButton = new JButton();
32    JButton prevButton = new JButton();
33    JButton addPathButton = new JButton();
34    JButton quitButton = new JButton();
35    JScrollPane sourcePathScrollPane = new JScrollPane();
36    JList javaPathList = new JList();
37    JLabel pathListLabel = new JLabel();
38    JButton removeButton = new JButton();
39    JTextField javaPathField = new JTextField();
40    JButton chooseJavaPathButton = new JButton();
41    JLabel pathFieldLabel = new JLabel();
42  
43    Vector sourcePaths = new Vector();
44  
45    public JavaSourcePathsPanel(MainWindow mainWindow)
46    {
47      try
48      {
49        this.mainWindow = mainWindow;
50        sourcePaths.removeAllElements();
51        jbInit();
52      }
53      catch(Exception ex)
54      {
55        ex.printStackTrace();
56      }
57    }
58  
59    public JavaSourcePathsPanel()
60    {
61      this(null);
62    }
63  
64    public void setMainWindow(MainWindow mainWindow) {
65      this.mainWindow = mainWindow;
66    }
67  
68    private void jbInit() throws Exception
69    {
70      this.setLayout(null);
71      this.setPreferredSize(new Dimension(600, 400));
72  
73      prevButton.setText("<<Previous");
74      prevButton.setBounds(new Rectangle(270 ,360, 100, 30));
75      prevButton.addActionListener(new java.awt.event.ActionListener()
76      {
77  
78        public void actionPerformed(ActionEvent e)
79        {
80          prevButton_actionPerformed(e);
81        }
82      });
83  
84      nextButton.setText("Next >>");
85      nextButton.setBounds(new Rectangle(385, 360, 100, 30));
86      nextButton.addActionListener(new java.awt.event.ActionListener()
87      {
88  
89        public void actionPerformed(ActionEvent e)
90        {
91          nextButton_actionPerformed(e);
92        }
93      });
94  
95      helpButton.setText("Help");
96      helpButton.setBounds(new Rectangle(500, 360, 75, 30));
97      helpButton.addActionListener(new java.awt.event.ActionListener()
98      {
99  
100       public void actionPerformed(ActionEvent e)
101       {
102         helpButton_actionPerformed(e);
103       }
104     });
105 
106     pathFieldLabel.setText("Please enter a Java source path or filename");
107     pathFieldLabel.setBounds(new Rectangle(25, 285, 450, 25));
108 
109     pathListLabel.setText("Java source paths / files");
110     pathListLabel.setBounds(new Rectangle(25, 15, 450, 25));
111     sourcePathScrollPane.setBounds(new Rectangle(25, 40, 425, 230));
112     javaPathList.addKeyListener(new java.awt.event.KeyListener() {
113       public void keyPressed(KeyEvent e) {}
114       public void keyTyped(KeyEvent e) {}
115       public void keyReleased(KeyEvent e) {
116         if (e.getKeyCode()==KeyEvent.VK_DELETE) {
117         }
118       }
119     });
120 
121 
122     removeButton.setText("Remove");
123     removeButton.setBounds(new Rectangle(475, 245, 100, 25));
124     removeButton.addActionListener(new java.awt.event.ActionListener()
125     {
126 
127       public void actionPerformed(ActionEvent e)
128       {
129         removeButton_actionPerformed(e);
130       }
131     });
132 
133     javaPathField.setBounds(new Rectangle(25, 310, 400, 25));
134     javaPathField.addKeyListener(new java.awt.event.KeyListener() {
135       public void keyPressed(KeyEvent e) {}
136       public void keyTyped(KeyEvent e) {}
137       public void keyReleased(KeyEvent e) {
138         if (e.getKeyCode()==KeyEvent.VK_ENTER) {
139           addPathButton_actionPerformed(null);
140         }
141       }
142     });
143 
144     chooseJavaPathButton.setText("...");
145     chooseJavaPathButton.setBounds(new Rectangle(425, 310, 25, 25));
146     chooseJavaPathButton.addActionListener(new java.awt.event.ActionListener()
147     {
148 
149       public void actionPerformed(ActionEvent e)
150       {
151         chooseJavaPathButton_actionPerformed(e);
152       }
153     });
154 
155     addPathButton.setText("Add");
156     addPathButton.setBounds(new Rectangle(475, 310, 100, 25));
157     addPathButton.addActionListener(new java.awt.event.ActionListener()
158     {
159 
160       public void actionPerformed(ActionEvent e)
161       {
162         addPathButton_actionPerformed(e);
163       }
164     });
165 
166     quitButton.setText("Quit");
167     quitButton.setBounds(new Rectangle(25, 360, 150, 30));
168     quitButton.addActionListener(new java.awt.event.ActionListener()
169     {
170 
171       public void actionPerformed(ActionEvent e)
172       {
173         quitButton_actionPerformed(e);
174       }
175     });
176 
177     this.add(prevButton, null);
178     this.add(nextButton, null);
179     this.add(helpButton, null);
180     this.add(quitButton, null);
181     this.add(sourcePathScrollPane, null);
182     this.add(addPathButton, null);
183     this.add(pathListLabel, null);
184     this.add(removeButton, null);
185     this.add(javaPathField, null);
186     this.add(chooseJavaPathButton, null);
187     this.add(pathFieldLabel, null);
188     sourcePathScrollPane.getViewport().add(javaPathList, null);
189 
190   }
191 
192   /** Updates the look&feel of the given component. Subcomponents are
193     * recursively updated.
194     *
195     * @param component the component to update its look&feel
196     */
197   protected void updateComponentUI(JComponent component) {
198     component.updateUI();
199     Component[] components = component.getComponents();
200     for (int i=0; i<components.length; i++) {
201       if(components[i] instanceof JComponent) {
202         updateComponentUI((JComponent) components[i]);
203       }
204     }
205   }
206 
207   /** Updates labels of all buttons */
208   protected void updateLabels() {
209     quitButton.setText(CodeMapper.getText(LabelCode.QUIT_BUTTON));
210     helpButton.setText(CodeMapper.getText(LabelCode.HELP_BUTTON));
211     prevButton.setText(CodeMapper.getText(LabelCode.PREVIOUS_BUTTON));
212     nextButton.setText(CodeMapper.getText(LabelCode.NEXT_BUTTON));
213     removeButton.setText(CodeMapper.getText(LabelCode.REMOVE_BUTTON));
214     addPathButton.setText(CodeMapper.getText(LabelCode.ADD_BUTTON));
215     pathListLabel.setText(CodeMapper.getText(LabelCode.JAVASOURCELIST_LABEL));
216     pathFieldLabel.setText(CodeMapper.getText(LabelCode.JAVASOURCEFIELD_LABEL));
217   }
218 
219   public void updateAll() {
220     updateLabels();
221     updateComponentUI(this);
222   }
223 
224   public boolean panelActivated(boolean fromPreviousPanel)
225   {
226     setWindowTitle();
227     if (mainWindow.getCurrentProject().getJavaSourcePath()!=null) {
228       sourcePaths = mainWindow.getCurrentProject().getJavaSourcePath();
229     }
230     javaPathList.setListData(sourcePaths);
231     return false;
232   }
233 
234   public boolean panelDeactivated(boolean toNextPanel)
235   {
236     if (!toNextPanel) return false;
237 
238     mainWindow.getCurrentProject().setJavaSourcePaths(sourcePaths);
239     if (sourcePaths.isEmpty()) {
240       Main.messages.println("No source files/directories selected");
241       return true;
242     }
243     MOPAccess mopAccess = GlobalSettings.mopAccess;
244     ParseMessageDialog msgDialog = null;
245     if (!mainWindow.isQuietMode()) msgDialog = new ParseMessageDialog(mainWindow);
246     mainWindow.setWaitCursor();
247     Vector mopSourcePaths = mainWindow.getCurrentProject().getMOPSourcePaths();
248     Vector absoluteSourcePaths = new Vector(mopSourcePaths.size());
249     Enumeration pathEnum = mopSourcePaths.elements();
250     while (pathEnum.hasMoreElements()) {
251       String relativePath = (String) pathEnum.nextElement();
252       String absolutePath = GlobalSettings.getAbsolutePath(mainWindow.getCurrentProject().getProjectPath(), relativePath);
253       absoluteSourcePaths.addElement(absolutePath);
254     }
255     boolean result = result = mopAccess.parseJavaSourceFiles(absoluteSourcePaths,getFilenameFilter(), msgDialog);
256     mainWindow.setDefaultCursor();
257     if (!result) return true;
258 
259     if (mopAccess.getClassCount()==0) {
260       if (!mainWindow.isQuietMode()) {
261         MessageDialog dlg = new MessageDialog(mainWindow,CodeMapper.getText(LabelCode.ERROR_LABEL), true);
262         dlg.setMessage(CodeMapper.getText(ErrorCode.NOPARSEDCLASSES_TEXT));
263         dlg.show();
264       }
265       else {
266         Main.log(CodeMapper.getText(LabelCode.ERROR_LABEL)+": "+CodeMapper.getText(ErrorCode.NOPARSEDCLASSES_TEXT));
267       }
268       return true;
269     }
270     return false;
271   }
272 
273   public void setWindowTitle()
274   {
275     mainWindow.setTitle(CodeMapper.getText(LabelCode.JAVASOURCEPATHS_TITLE));
276   }
277 
278   public void newProjectCreated()
279   {
280     sourcePaths.removeAllElements();
281     javaPathList.setListData(sourcePaths);
282   }
283 
284   void prevButton_actionPerformed(ActionEvent e)
285   {
286     mainWindow.switchToPrevious();
287   }
288 
289   void nextButton_actionPerformed(ActionEvent e)
290   {
291     mainWindow.switchToNext();
292   }
293 
294   void removeButton_actionPerformed(ActionEvent e)
295   {
296     Object selectedValues[] = javaPathList.getSelectedValues();
297     for (int i=0; i<selectedValues.length; i++) {
298       sourcePaths.removeElement(selectedValues[i]);
299     }
300     javaPathList.setListData(sourcePaths);
301     mainWindow.getCurrentProject().setChanged();
302   }
303 
304   void addPathButton_actionPerformed(ActionEvent e)
305   {
306     String fileName = javaPathField.getText();
307     if (fileName==null || fileName.equals("")) return;
308     File file = new File(fileName);
309 
310     if (!file.isAbsolute() && mainWindow.getCurrentProject()!=null) {
311       file = new File(GlobalSettings.getAbsolutePath(mainWindow.getCurrentProject().getProjectPath(), fileName));
312     }
313 
314     if (!file.exists()) {
315       String errorTitle = CodeMapper.getText(LabelCode.ERROR_LABEL);
316       MessageDialog dlg = new MessageDialog(mainWindow,errorTitle,true,false);
317       String msgText = CodeMapper.getText(ErrorCode.FILENOTEXISTS_ERROR);
318       msgText = StringUtil.replace(msgText,"%1",fileName);
319       dlg.setMessage(msgText,false);
320       dlg.show();
321       javaPathField.selectAll();
322       return;
323     }
324     if (!isReachable(file)) {
325       Main.err.println("File '"+file.getAbsoluteFile()+"' not reachable through SOURCEPATH.");
326       Main.err.println("SOURCEPATH is:");
327       Vector rootPaths = mainWindow.getCurrentProject().getMOPSourcePaths();
328       if (rootPaths!=null && !rootPaths.isEmpty()) {
329         Enumeration enum = rootPaths.elements();
330         while (enum.hasMoreElements()) {
331           Main.err.print("   "+((String) enum.nextElement()));
332         }
333       }
334       else Main.err.println("<empty>");
335       javaPathField.selectAll();
336       return;
337     }
338     if (mainWindow.getCurrentProject()!=null) {
339       fileName = GlobalSettings.getRelativePath(mainWindow.getCurrentProject().getProjectPath(), fileName);
340     }
341     fileName = fileName.replace('\\','/');
342     if (file.isDirectory()) {
343       fileName = fileName.trim();
344       if (!fileName.endsWith(File.separator)) fileName = fileName + "/";
345       if (!sourcePaths.contains(fileName)) {
346         sourcePaths.addElement(fileName);
347         mainWindow.getCurrentProject().setChanged();
348       }
349     }
350     else {
351       if (!fileName.toLowerCase().endsWith(".java")) {
352         // error: not a *.java file
353         javaPathField.selectAll();
354         return;
355       }
356       if (!sourcePaths.contains(fileName)) {
357         sourcePaths.addElement(fileName);
358         mainWindow.getCurrentProject().setChanged();
359       }
360 
361     }
362     javaPathList.setListData(sourcePaths);
363     javaPathField.setText("");
364   }
365 
366   void chooseJavaPathButton_actionPerformed(ActionEvent e)
367   {
368     JFileChooser fileChooser;
369     if (GlobalSettings.javaBaseDir!=null) fileChooser = new JFileChooser(GlobalSettings.javaBaseDir);
370     else fileChooser = new JFileChooser();
371     InjectJFileFilter fileFilter = new InjectJFileFilter("java",
372       CodeMapper.getText(LabelCode.JAVAFILE_DESCRIPT));
373     fileChooser.addChoosableFileFilter(fileFilter);
374     fileChooser.setFileFilter(fileFilter);
375     fileChooser.setMultiSelectionEnabled(false);
376     fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
377 
378 
379     int result = fileChooser.showOpenDialog(this);
380     if (result==JFileChooser.APPROVE_OPTION) {
381       File file = fileChooser.getSelectedFile();
382       String name = "";
383       try {
384         if (file.isDirectory()) {
385           name = file.getCanonicalPath();
386         }
387         else if (file.exists() && file.isFile()) {
388           name = file.getCanonicalPath();
389         }
390       }
391       catch (IOException ioe) {
392         ioe.printStackTrace();
393       }
394 
395       name = name.replace('\\','/');
396       if(name!=null && (!name.equals(""))) {
397         javaPathField.setText(name);
398         addPathButton_actionPerformed(null);
399       }
400     }
401   }
402 
403   /** Checks if file is reachabe through current MOP searchpath.
404     *
405     * @param file file to check if it is reachable through the MOP searchpath
406     * @return true, if file is reachable, false otherwise
407     */
408   protected boolean isReachable(File file) {
409     String path = "";
410     try {
411       path = file.getCanonicalPath();
412     }
413     catch (IOException e) {
414       e.printStackTrace();
415       return false;
416     }
417 
418     if (file.isDirectory() && !path.endsWith(File.separator)) {
419       path = path + File.separator;
420     }
421     path = path.replace('\\','/');
422 
423     String projectPath = mainWindow.getCurrentProject().getProjectPath();
424     Vector mopRootPaths = mainWindow.getCurrentProject().getMOPSourcePaths();
425     Enumeration enum = mopRootPaths.elements();
426 
427 /*
428     if (rootPaths==null) {
429 
430       rootPaths = GlobalSettings.javaSourceRootPaths;
431       if (rootPaths==null) {
432       // Get root paths from Compost...
433         rootPaths = new String[1];
434         rootPaths[0] = GlobalSettings.javaBaseDir;
435         GlobalSettings.javaSourceRootPaths = rootPaths;
436       }
437       String projectPath = mainWindow.getCurrentProject().getProjectPath();
438       for (int j=0; j<rootPaths.length; j++) {
439         rootPaths[j] = GlobalSettings.getAbsolutePath(projectPath, rootPaths[j]);
440       }
441     }
442 */
443 //    for (int i=0; i<rootPaths.length; i++) {
444     while(enum.hasMoreElements()) {
445       File temp = new File((String) enum.nextElement());
446       String rootPath = null;
447       try {
448         rootPath = temp.getCanonicalPath();
449       }
450       catch (IOException e) { continue; }
451       rootPath = rootPath.replace('\\','/');
452       if (path.startsWith(rootPath)) return true;
453       if (path.toLowerCase().startsWith(rootPath.toLowerCase())) {
454         if (GlobalSettings.ignoreCase) return true;
455       }
456     }
457     return false;
458   }
459 
460   /** Creates an filename filer according to the seleted paths.
461     *
462     * @return the created filename filter
463     */
464   public FilenameFilter getFilenameFilter() {
465     return new FilenameFilter() {
466       public boolean accept(File dir, String fileName) {
467         if (fileName==null) {
468           Main.err.println("No filename!");
469           return false;
470         }
471         String filePath = "";
472         if (dir!=null) {
473           try {
474             filePath = dir.getCanonicalPath();
475             if (!filePath.endsWith(File.separator)) filePath = filePath + File.separator;
476           }
477           catch (IOException e) {};
478         }
479         if (filePath==null) filePath="";
480         if (GlobalSettings.ignoreCase) {
481           filePath = filePath.toLowerCase();
482           fileName = fileName.toLowerCase();
483         }
484         filePath = filePath.replace('\\','/');
485         fileName = fileName.replace('\\','/');
486         if (!fileName.endsWith(".java")) return false;
487         Enumeration enum = sourcePaths.elements();
488         while (enum.hasMoreElements()) {
489           String acceptPath = (String) enum.nextElement();
490           if (mainWindow.getCurrentProject()!=null) {
491             acceptPath = GlobalSettings.getAbsolutePath(mainWindow.getCurrentProject().getProjectPath(), acceptPath);
492           }
493           if (GlobalSettings.ignoreCase) acceptPath = acceptPath.toLowerCase();
494           if (acceptPath.endsWith(".java")) {
495             String fullPath = filePath;
496             if (!fullPath.endsWith("/")) fullPath = fullPath + "/";
497             fullPath = fullPath + fileName;
498             if (acceptPath.equals(fullPath)) {
499               return true;
500             }
501           }
502           else if (filePath.startsWith(acceptPath)) {
503             return true;
504           }
505         }
506         return false;
507       }
508     };
509   }
510 
511   void helpButton_actionPerformed(ActionEvent e)
512   {
513     mainWindow.showHelp();
514   }
515 
516   void quitButton_actionPerformed(ActionEvent e)
517   {
518     if(mainWindow!=null) mainWindow.dispose();
519     mainWindow.exit();
520   }
521 
522   public String helpFilename() {
523     return "sourcepaths.html";
524   }
525 
526 }