/plugins/ProjectViewer/tags/pv_2_1_3_3/projectviewer/config/ProjectPropertiesPane.java

# · Java · 335 lines · 233 code · 54 blank · 48 comment · 39 complexity · 95dbaab6050db90a4a15beab24d72935 MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.config;
  20. //{{{ Imports
  21. import java.io.File;
  22. import java.awt.Dimension;
  23. import java.awt.GridBagLayout;
  24. import java.awt.GridBagConstraints;
  25. import java.awt.Insets;
  26. import java.awt.Point;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import javax.swing.JLabel;
  30. import javax.swing.JButton;
  31. import javax.swing.JTextField;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JFileChooser;
  34. import javax.swing.JPopupMenu;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.Buffer;
  37. import org.gjt.sp.jedit.AbstractOptionPane;
  38. import projectviewer.ProjectManager;
  39. import projectviewer.gui.GroupMenu;
  40. import projectviewer.gui.ModalJFileChooser;
  41. import projectviewer.vpt.VPTGroup;
  42. import projectviewer.vpt.VPTProject;
  43. import projectviewer.vpt.VPTRoot;
  44. //}}}
  45. /**
  46. * A dialog for configuring the properties of a project.
  47. *
  48. * @author Marcelo Vanzin
  49. * @author Matt Payne (made slight changes for urlRoot)
  50. */
  51. public class ProjectPropertiesPane extends AbstractOptionPane implements ActionListener {
  52. public final static String DEFAULT_URL = "http://";
  53. //{{{ Instance Variables
  54. private int result;
  55. private String lookupPath;
  56. private VPTProject project;
  57. private JTextField projName;
  58. private JTextField projRoot;
  59. private JTextField projURLRoot;
  60. private JButton chooseRoot;
  61. private JButton chooseGroup;
  62. private JPopupMenu groupPopupMenu;
  63. private boolean ok;
  64. private boolean isNew;
  65. //}}}
  66. //{{{ Constructors
  67. /** Builds the dialog. */
  68. public ProjectPropertiesPane(VPTProject p, boolean isNew) {
  69. this(p, isNew, null);
  70. }
  71. public ProjectPropertiesPane(VPTProject p, boolean isNew, String lookupPath) {
  72. super("projectviewer.project_props");
  73. this.project = p;
  74. this.ok = true;
  75. this.isNew = isNew;
  76. this.lookupPath = lookupPath;
  77. }
  78. //}}}
  79. //{{{ actionPerformed(ActionEvent) method
  80. /**
  81. * Shows a file chooser so the user can choose the root directory of
  82. * its project. In case the user chooses a directory, the corresponding
  83. * JTextField is updated to show the selection.
  84. */
  85. public void actionPerformed(ActionEvent ae) {
  86. if (ae.getSource() == chooseRoot) {
  87. JFileChooser chooser = new ModalJFileChooser();
  88. chooser.setDialogTitle(jEdit.getProperty("projectviewer.project.options.root_dialog"));
  89. chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  90. String root = projRoot.getText().trim();
  91. if (root.length() > 0) {
  92. chooser.setSelectedFile(new File(root));
  93. chooser.setCurrentDirectory(new File(root).getParentFile());
  94. } else if (lookupPath != null) {
  95. File f = new File(lookupPath);
  96. if (!f.isDirectory()) {
  97. f = f.getParentFile();
  98. }
  99. chooser.setCurrentDirectory(f.getParentFile());
  100. } else {
  101. Buffer b = jEdit.getActiveView().getBuffer();
  102. chooser.setCurrentDirectory(new File(b.getPath()).getParentFile());
  103. }
  104. if (chooser.showDialog(this, jEdit.getProperty("projectviewer.general.choose"))
  105. == JFileChooser.APPROVE_OPTION) {
  106. root = chooser.getSelectedFile().getAbsolutePath();
  107. projRoot.setText(root);
  108. projRoot.setToolTipText(projRoot.getText());
  109. if (projName.getText() != null && projName.getText().length() == 0) {
  110. String name = root.substring(root.lastIndexOf(File.separator) + 1, root.length());
  111. projName.setText(name);
  112. }
  113. }
  114. } else if (ae.getSource() == chooseGroup) {
  115. if (groupPopupMenu == null) {
  116. GroupMenu gm = new GroupMenu(null, false, false, this);
  117. groupPopupMenu = new JPopupMenu();
  118. gm.populate(groupPopupMenu, VPTRoot.getInstance(), jEdit.getActiveView());
  119. }
  120. Point p = chooseGroup.getLocation();
  121. groupPopupMenu.show(this, (int) p.getX(), (int) p.getY() + chooseGroup.getHeight());
  122. } else if (ae.getSource() instanceof VPTGroup) {
  123. project.setProperty("projectviewer.new-parent", ae.getSource());
  124. chooseGroup.setText(((VPTGroup)ae.getSource()).getName());
  125. groupPopupMenu.setVisible(false);
  126. }
  127. } //}}}
  128. //{{{ _save() method
  129. /** Updates the project with the info supplied by the user. */
  130. protected void _save() {
  131. String name = projName.getText().trim();
  132. ok = true;
  133. if (name.length() == 0) {
  134. JOptionPane.showMessageDialog(
  135. this,
  136. jEdit.getProperty("projectviewer.project.options.no_name"),
  137. jEdit.getProperty("projectviewer.project.options.error.title"),
  138. JOptionPane.ERROR_MESSAGE
  139. );
  140. ok = false;
  141. }
  142. if (isNew && ProjectManager.getInstance().hasProject(name)) {
  143. JOptionPane.showMessageDialog(
  144. this,
  145. jEdit.getProperty("projectviewer.project.options.name_exists"),
  146. jEdit.getProperty("projectviewer.project.options.error.title"),
  147. JOptionPane.ERROR_MESSAGE
  148. );
  149. ok = false;
  150. }
  151. String root = projRoot.getText().trim();
  152. if (root.length() == 0) {
  153. JOptionPane.showMessageDialog(
  154. this,
  155. jEdit.getProperty("projectviewer.project.options.no_root"),
  156. jEdit.getProperty("projectviewer.project.options.error.title"),
  157. JOptionPane.ERROR_MESSAGE
  158. );
  159. ok = false;
  160. } else if (!(new File(root).exists())) {
  161. JOptionPane.showMessageDialog(
  162. this,
  163. jEdit.getProperty("projectviewer.project.options.root_error"),
  164. jEdit.getProperty("projectviewer.project.options.error.title"),
  165. JOptionPane.ERROR_MESSAGE
  166. );
  167. ok = false;
  168. }
  169. String urlRoot = projURLRoot.getText().trim();
  170. if (ok) {
  171. project.setName(name);
  172. project.setRootPath(root);
  173. if (urlRoot.length() != 0 && !urlRoot.equals(DEFAULT_URL)) {
  174. project.setURL(urlRoot);
  175. } else {
  176. project.setURL(null);
  177. }
  178. }
  179. } //}}}
  180. //{{{ _init() method
  181. /** Load the GUI components of the dialog. */
  182. protected void _init() {
  183. // Builds the dialog
  184. GridBagLayout gridbag = new GridBagLayout();
  185. GridBagConstraints gc = new GridBagConstraints();
  186. gc.fill = GridBagConstraints.HORIZONTAL;
  187. gc.insets = new Insets(3,3,3,3);
  188. setLayout(gridbag);
  189. // Project name
  190. JLabel label = new JLabel(jEdit.getProperty("projectviewer.project.options.name"));
  191. gc.weightx = 0;
  192. gc.gridx = 0;
  193. gc.gridy = 0;
  194. gc.gridwidth = 1;
  195. gridbag.setConstraints(label,gc);
  196. add(label);
  197. projName = new JTextField();
  198. projName.setText(project.getName());
  199. gc.weightx = 1;
  200. gc.gridx = 1;
  201. gc.gridy = 0;
  202. gc.gridwidth = 2;
  203. gridbag.setConstraints(projName,gc);
  204. add(projName);
  205. // Project root
  206. label = new JLabel(jEdit.getProperty("projectviewer.project.options.root"));
  207. gc.weightx = 0;
  208. gc.gridx = 0;
  209. gc.gridy = 1;
  210. gc.gridwidth = 1;
  211. gridbag.setConstraints(label,gc);
  212. add(label);
  213. projRoot = new JTextField();
  214. projRoot.setText(project.getRootPath());
  215. projRoot.setToolTipText(projRoot.getText());
  216. projRoot.setPreferredSize(
  217. new Dimension(50, (int)projRoot.getPreferredSize().getHeight())
  218. );
  219. gc.weightx = 1;
  220. gc.gridx = 1;
  221. gc.gridy = 1;
  222. gc.gridwidth = 1;
  223. gridbag.setConstraints(projRoot,gc);
  224. add(projRoot);
  225. chooseRoot = new JButton(jEdit.getProperty("projectviewer.project.options.root_choose"));
  226. chooseRoot.addActionListener(this);
  227. gc.weightx = 0;
  228. gc.gridx = 2;
  229. gc.gridy = 1;
  230. gc.gridwidth = 1;
  231. gridbag.setConstraints(chooseRoot,gc);
  232. add(chooseRoot);
  233. // URL Root for web projects. Used to launch files in web browser against webserver
  234. label = new JLabel(jEdit.getProperty("projectviewer.project.options.url_root"));
  235. gc.weightx = 0;
  236. gc.gridx = 0;
  237. gc.gridy = 2;
  238. gc.gridwidth = 1;
  239. gridbag.setConstraints(label, gc);
  240. add(label);
  241. projURLRoot = new JTextField();
  242. projURLRoot.setToolTipText(jEdit.getProperty("projectviewer.project.options.url_root.tooltip"));
  243. if (project.getURL() != null) {
  244. projURLRoot.setText(project.getURL());
  245. projURLRoot.setToolTipText(project.getURL());
  246. } else {
  247. projURLRoot.setText(DEFAULT_URL);
  248. projURLRoot.setToolTipText(DEFAULT_URL);
  249. }
  250. gc.weightx = 1;
  251. gc.gridx = 1;
  252. gc.gridy = 2;
  253. gc.gridwidth = 2;
  254. gridbag.setConstraints(projURLRoot, gc);
  255. add(projURLRoot);
  256. // The group where the project will be attached
  257. label = new JLabel(jEdit.getProperty("projectviewer.project.options.parent_group"));
  258. gc.weightx = 0;
  259. gc.gridx = 0;
  260. gc.gridy = 3;
  261. gc.gridwidth = 1;
  262. gridbag.setConstraints(label, gc);
  263. add(label);
  264. VPTGroup parent = (VPTGroup) project.getParent();
  265. if (parent == null) {
  266. parent = VPTRoot.getInstance();
  267. }
  268. chooseGroup = new JButton(parent.getName());
  269. gc.weightx = 1;
  270. gc.gridx = 1;
  271. gc.gridy = 3;
  272. gc.gridwidth = 2;
  273. gridbag.setConstraints(chooseGroup, gc);
  274. chooseGroup.addActionListener(this);
  275. add(chooseGroup);
  276. // finish
  277. setPreferredSize(new Dimension(300,250));
  278. } //}}}
  279. //{{{ isOK() method
  280. protected boolean isOK() { return ok; } //}}}
  281. }