1   /**
2    * QJ-Pro
3    * Copyright (c) 2004, http://qjpro.sourceforge.net
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package com.qasystems.qstudio.java.gui.dialog;
20  
21  import com.qasystems.international.MessageResource;
22  import com.qasystems.qstudio.java.QStudioGlobal;
23  import com.qasystems.qstudio.java.browser.BrowserHelpContext;
24  import com.qasystems.qstudio.java.gui.CheckPropertiesControl;
25  import com.qasystems.qstudio.java.gui.GeneralPropertiesControl;
26  import com.qasystems.qstudio.java.gui.Project;
27  import com.qasystems.qstudio.java.gui.ProjectPropertiesPane;
28  import com.qasystems.qstudio.java.gui.RegExpr;
29  import com.qasystems.qstudio.java.gui.RuleSet;
30  
31  import java.awt.Dimension;
32  import java.io.File;
33  import java.util.Vector;
34  
35  import javax.swing.JTabbedPane;
36  import javax.swing.event.ChangeEvent;
37  import javax.swing.event.ChangeListener;
38  
39  /**
40   * This class implements the ProjectProperties Dialog window.
41   */
42  public class ProjectPropertiesDialog extends HelpButtonDialog {
43    private static final MessageResource RESOURCES =
44      MessageResource.getClientInstance();
45  
46    /** value when OK button was chosen */
47    public final static int OK = 0;
48  
49    /** value when Cancel button was chosen */
50    public final static int CANCEL = 1;
51    private final static String TITLE = RESOURCES.getString("WINDOWTITLE_029");
52    private final static String[] BUTTONS =
53    { RESOURCES.getString("BUTTON_005"), RESOURCES.getString("BUTTON_001") };
54    private ProjectPropertiesPane propertiesPane = null;
55    private BrowserHelpContext helpContext = new BrowserHelpContext();
56  
57    /**
58     * Default constructor.
59     */
60    public ProjectPropertiesDialog() {
61      super(new QStudioGlobal().getFrame());
62      jbInit();
63      pack();
64    }
65  
66    /**
67     * override, limit size of the dialog
68     */
69    public void pack() {
70      super.pack();
71      setSize(getMyPrefferedSize());
72    }
73  
74    /**
75     * - override superclass reaction to WindowClose event
76     */
77    protected void cancelDialog() {
78      getChecksTabControl().cleanup();
79      super.cancelDialog();
80    }
81  
82    /**
83     * - override superclass button activation
84     * @param action the action chosen (ok or cancel)
85     */
86    protected void onActionPerformed(int action) {
87      if (action == OK) {
88        checkRegularExpressions(action);
89      } else {
90        getChecksTabControl().cleanup();
91        super.onActionPerformed(action);
92      }
93    }
94  
95    private void checkRegularExpressions(int action) {
96      final RegExpr regExpr = new RegExpr(this);
97  
98      if (
99        propertiesPane.getProductIdentification().equals(
100           Project.LOCAL_PRODUCT_ID
101         )
102     ) {
103       regExpr.setExtendedErrorMessage(true);
104     }
105 
106     regExpr.checkRegularExpressions(propertiesPane.getRules());
107 
108     if (regExpr.isCancel()) {
109       setResultValue(CANCEL);
110       onActionPerformed(CANCEL);
111     } else if (regExpr.isOK()) {
112       getChecksTabControl().cleanup();
113       super.onActionPerformed(action);
114     } else {
115       //Intentionally left blank
116     }
117   }
118 
119   /**
120    * Loads the settings of a given QStudio project into the dialog.
121    *
122    * @param project the project whose properties should be displayed
123    */
124   public void upload(Project project) {
125     propertiesPane.upload(project);
126   }
127 
128   /**
129    * Load the settings from the dialog into a project.
130    *
131    * @param project the QStudio project to load the settings into
132    */
133   public void download(Project project) {
134     propertiesPane.download(project);
135   }
136 
137   /**
138    * Sets the list of rules.
139    *
140    * @param rules a rules set
141    */
142   public synchronized void setRules(RuleSet rules) {
143     propertiesPane.setRules(rules);
144   }
145 
146   /**
147    * Gets the list of rules.
148    *
149    * @return a rules set
150    */
151   public RuleSet getRules() {
152     return (propertiesPane.getRules());
153   }
154 
155   /**
156    * Set the CQC Standards, only implemented for Enterprise version
157    *
158    * @param newRuleSets the CQC standards
159    */
160   public synchronized void setStandards(Object newRuleSets) {
161     propertiesPane.setStandards(newRuleSets);
162   }
163 
164   /**
165    * Sets the source path.
166    *
167    * @param paths the path
168    */
169   public void setSourcePath(Vector paths) {
170     propertiesPane.setSourcePath(paths);
171   }
172 
173   /**
174    * Gets the source path.
175    *
176    * @return the path (never <tt>null</tt>)
177    */
178   public Vector getSourcePath() {
179     return (propertiesPane.getSourcePath());
180   }
181 
182   /**
183    * Sets the class path.
184    *
185    * @param paths the path
186    */
187   public void setClassPath(Vector paths) {
188     propertiesPane.setClassPath(paths);
189   }
190 
191   /**
192    * Gets the class path.
193    *
194    * @return the path
195    */
196   public Vector getClassPath() {
197     return (propertiesPane.getClassPath());
198   }
199 
200   /**
201    * Sets the output path.
202    *
203    * @param newPath the directory
204    */
205   public void setOutputPath(File newPath) {
206     propertiesPane.setOutputPath(newPath);
207   }
208 
209   /**
210    * Gets the output path.
211    *
212    * @return the directory
213    */
214   public File getOutputPath() {
215     return (propertiesPane.getOutputPath());
216   }
217 
218   /**
219    * Sets the project name.
220    *
221    * @param name the name
222    */
223   public void setProjectName(String name) {
224     propertiesPane.getGeneralTabControl().setName(name);
225   }
226 
227   /**
228    * Gets the project name.
229    *
230    * @return the name
231    */
232   public String getProjectName() {
233     return (propertiesPane.getGeneralTabControl().getName());
234   }
235 
236   /**
237    * Sets the project's source code Java version.
238    *
239    * @param version the version
240    */
241   public void setJavaVersion(String version) {
242     propertiesPane.getGeneralTabControl().setJavaVersion(version);
243   }
244 
245   /**
246    * Gets the project's source code Java version.
247    *
248    * @return the name
249    */
250   public String getJavaVersion() {
251     return (propertiesPane.getGeneralTabControl().getJavaVersion());
252   }
253 
254   /**
255    * Sets the visibility of the given tab panel.
256    *
257    * @param tab the name on the tab,
258    *            e.g. ProjectPropertiesDialog.PATHS_TAB
259    * @param visible if <tt>true</tt> the tab panel is made visible
260    */
261   public void setTabVisible(String tab, boolean visible) {
262     propertiesPane.setTabVisible(tab, visible);
263   }
264 
265   /**
266    * Get the tab for the general properties
267    *
268    * @return the current tab
269    */
270   public GeneralPropertiesControl getGeneralTabControl() {
271     return (propertiesPane.getGeneralTabControl());
272   }
273 
274   /**
275    * Get the control for editing a ruleset
276    *
277    * @return get the control
278    */
279   public CheckPropertiesControl getChecksTabControl() {
280     return (propertiesPane.getChecksTabControl());
281   }
282 
283   /**
284    * Show the desired tab
285    *
286    * @param tab the tab
287    */
288   public void displayTab(String tab) {
289     propertiesPane.displayTab(tab);
290   }
291 
292   private synchronized void jbInit() {
293     setModal(true);
294     setResizable(true);
295     setTitle(TITLE);
296     setButtons(BUTTONS);
297     setInitialButton(OK);
298     initPropertiesPane();
299     setContent(propertiesPane);
300   }
301 
302   /**
303    * Initialize the content of the window, Enterprise has
304    * more functionality.
305    */
306   private void initPropertiesPane() {
307     propertiesPane = new ProjectPropertiesPane(this);
308     helpContext.setContext(BrowserHelpContext.HELP_TOUR_5);
309     setHelpContext(helpContext);
310     propertiesPane.addChangeListener(
311       new ChangeListener() {
312         public void stateChanged(ChangeEvent e) {
313           final JTabbedPane pane = (JTabbedPane) e.getSource();
314           final String title = pane.getTitleAt(pane.getSelectedIndex());
315 
316           if (ProjectPropertiesPane.GENERAL_TAB.equals(title)) {
317             helpContext.setContext(BrowserHelpContext.HELP_TOUR_5);
318             setHelpContext(helpContext);
319           } else if (ProjectPropertiesPane.PATHS_TAB.equals(title)) {
320             helpContext.setContext(BrowserHelpContext.HELP_TOUR_3);
321             setHelpContext(helpContext);
322           } else if (ProjectPropertiesPane.CHECKS_TAB.equals(title)) {
323             helpContext.setContext(BrowserHelpContext.HELP_TOUR_7);
324             setHelpContext(helpContext);
325           } else {
326             helpContext.setContext(BrowserHelpContext.HELP);
327             setHelpContext(helpContext);
328           }
329         }
330       }
331     );
332   }
333 
334   private Dimension getMyPrefferedSize() {
335     return (new Dimension(750, 600));
336   }
337 }
338