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

Quick Search    Search Deep

Source code: org/gjt/sp/jedit/gui/DirectoryMenu.java


1   /*
2    * DirectoryMenu.java - File list menu
3    * :tabSize=8:indentSize=8:noTabs=false:
4    * :folding=explicit:collapseFolds=1:
5    *
6    * Copyright (C) 2000, 2001, 2002 Slava Pestov
7    *
8    * This program is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License
10   * as published by the Free Software Foundation; either version 2
11   * of the License, or any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21   */
22  
23  package org.gjt.sp.jedit.gui;
24  
25  //{{{ Imports
26  import javax.swing.event.*;
27  import javax.swing.*;
28  import java.awt.event.*;
29  import java.io.File;
30  import org.gjt.sp.jedit.browser.*;
31  import org.gjt.sp.jedit.io.FileVFS;
32  import org.gjt.sp.jedit.*;
33  //}}}
34  
35  public class DirectoryMenu extends EnhancedMenu
36  {
37    //{{{ DirectoryMenu constructor
38    public DirectoryMenu(String name, String dir)
39    {
40      super(name);
41      this.dir = dir;
42    } //}}}
43  
44    //{{{ menuSelected() method
45    public void menuSelected(MenuEvent evt)
46    {
47      super.menuSelected(evt);
48  
49      final View view = GUIUtilities.getView(this);
50  
51      if(getMenuComponentCount() != 0)
52        removeAll();
53  
54      final String path;
55      if(dir == null)
56      {
57        path = view.getBuffer().getDirectory();
58      }
59      else
60        path = dir;
61  
62      JMenuItem mi = new JMenuItem(path + ":");
63      mi.setActionCommand(path);
64      mi.setIcon(FileCellRenderer.openDirIcon);
65  
66      //{{{ ActionListeners
67      ActionListener fileListener = new ActionListener()
68      {
69        public void actionPerformed(ActionEvent evt)
70        {
71          jEdit.openFile(view,evt.getActionCommand());
72        }
73      };
74  
75      ActionListener dirListener = new ActionListener()
76      {
77        public void actionPerformed(ActionEvent evt)
78        {
79          VFSBrowser.browseDirectory(view,
80            evt.getActionCommand());
81        }
82      }; //}}}
83  
84      mi.addActionListener(dirListener);
85  
86      add(mi);
87      addSeparator();
88  
89      if(dir == null && !(view.getBuffer().getVFS() instanceof FileVFS))
90      {
91        mi = new JMenuItem(jEdit.getProperty(
92          "directory.not-local"));
93        mi.setEnabled(false);
94        add(mi);
95        return;
96      }
97  
98      File directory = new File(path);
99  
100     JMenu current = this;
101 
102     // for filtering out backups
103     String backupPrefix = jEdit.getProperty("backup.prefix");
104     String backupSuffix = jEdit.getProperty("backup.suffix");
105 
106     File[] list = directory.listFiles();
107     if(list == null || list.length == 0)
108     {
109       mi = new JMenuItem(jEdit.getProperty(
110         "directory.no-files"));
111       mi.setEnabled(false);
112       add(mi);
113     }
114     else
115     {
116       int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
117 
118       MiscUtilities.quicksort(list,
119         new MiscUtilities.StringICaseCompare());
120       for(int i = 0; i < list.length; i++)
121       {
122         File file = list[i];
123 
124         String name = file.getName();
125 
126         // skip marker files
127         if(name.endsWith(".marks"))
128           continue;
129 
130         // skip autosave files
131         if(name.startsWith("#") && name.endsWith("#"))
132           continue;
133 
134         // skip backup files
135         if((backupPrefix.length() != 0
136           && name.startsWith(backupPrefix))
137           || (backupSuffix.length() != 0
138           && name.endsWith(backupSuffix)))
139           continue;
140 
141         // skip directories
142         //if(file.isDirectory())
143         //  continue;
144 
145         mi = new JMenuItem(name);
146         mi.setActionCommand(file.getPath());
147         mi.addActionListener(file.isDirectory()
148           ? dirListener
149           : fileListener);
150         mi.setIcon(file.isDirectory()
151           ? FileCellRenderer.dirIcon
152           : FileCellRenderer.fileIcon);
153 
154         if(current.getItemCount() >= maxItems && i != list.length - 1)
155         {
156           //current.addSeparator();
157           JMenu newCurrent = new JMenu(
158             jEdit.getProperty(
159             "common.more"));
160           current.add(newCurrent);
161           current = newCurrent;
162         }
163         current.add(mi);
164       }
165     }
166   } //}}}
167 
168   public void menuDeselected(MenuEvent e) {}
169 
170   public void menuCanceled(MenuEvent e) {}
171 
172   //{{{ Private members
173   private String dir;
174   //}}}
175 }