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.gui.CheckPropertiesControl;
24  import com.qasystems.qstudio.java.gui.QStudioCheckPropertiesControl;
25  import com.qasystems.qstudio.java.gui.RuleSet;
26  import com.qasystems.swing.ButtonDialog;
27  import com.qasystems.util.Utilities;
28  
29  import java.awt.Dimension;
30  
31  import javax.swing.*;
32  
33  /**
34   * Support updating the Project with new and/or
35   * modified rule configurations.
36   *
37   */
38  public class ConfigurationMigrationDialog extends ButtonDialog {
39    /** representation of Ok button */
40    public final static int OK = 0;
41  
42    /** representaton of Cancel button */
43    public final static int CANCEL = 1;
44    private static final MessageResource RESOURCES =
45      MessageResource.getClientInstance();
46  
47    /**
48     * Due to sizing problems under different platforms
49     * line- and wordwrapping is no longer used.
50     *
51     * NOTE: the length of the strings must be equal/less
52     *       than the number of columns as set for
53     *       the JTextArea.
54     */
55    private static final String[] DEFAULT_MESSAGE =
56    { RESOURCES.getString("MESSAGE_098") };
57    private CheckPropertiesControl checksTabControl = null;
58    private CheckPropertiesControl pmdChecksTabControl = null;
59    private RuleSet ruleSet = null;
60    private JPanel panel = null;
61    private String[] warningMessage = DEFAULT_MESSAGE;
62  
63    /**
64     * Default constructor.
65     */
66    public ConfigurationMigrationDialog() {
67      super(new QStudioGlobal().getFrame());
68    }
69  
70    /**
71     * Create the gui, with the option of setting a special
72     * warning message
73     *
74     * @param message when null use the default warningmessage,
75     *        otherwise this message
76     */
77    public synchronized void createGui(String[] message) {
78      panel = new JPanel();
79  
80      if (message != null) {
81        setWarningMessage(message);
82      }
83  
84      try {
85        jbInit();
86      } catch (Exception e) {
87        e.printStackTrace();
88      }
89  
90      pack();
91  
92      setSize(getMyPrefferedSize());
93      setCenteredDialog(false);
94    }
95  
96    /**
97     * Sets the list of rules.
98     *
99     * @param rules a rules set
100    */
101   public synchronized void setRuleSet(RuleSet rules) {
102     ruleSet = rules;
103 
104     if (panel == null) {
105       createGui(null);
106     }
107 
108     checksTabControl.setRules(rules);
109     pmdChecksTabControl.setRules(rules);
110     pack();
111   }
112 
113   /**
114    * override, limit size of the dialog
115    */
116   public void pack() {
117     super.pack();
118     setSize(getMyPrefferedSize());
119   }
120 
121   /**
122    * Gets the list of rules.
123    *
124    * @return a rules set
125    */
126   public RuleSet getRuleSet() {
127     return (ruleSet);
128   }
129 
130   /**
131    * Cloning not allowed
132    *
133    * @return CloneNotSupportedException.
134    * @throws CloneNotSupportedException
135    */
136   public Object clone() throws CloneNotSupportedException {
137     throw new CloneNotSupportedException();
138   }
139 
140   /**
141    * Representation of this instance.
142    * @return representation
143    */
144   public String toString() {
145     return super.toString();
146   }
147 
148   /**
149    * Check for equality.
150    *
151    * @param obj the object to compare this object to
152    * @return <tt>true</tt> if this object equals obj
153    */
154   public boolean equals(Object obj) {
155     return (super.equals(obj));
156   }
157 
158   /**
159    * return the CheckPropertiesControl that holds the
160    * modified RuleConfigurations.
161    * NOTE: Used by the JBuilder integration
162    *
163    * @return the CheckPropertiesControl
164    */
165   public JComponent getControl() {
166     if (panel == null) {
167       createGui(null);
168     }
169 
170     return (panel);
171   }
172 
173   /**
174    * Free the resources held by the CheckPropertiesControl.
175    */
176   public void cleanup() {
177     checksTabControl.cleanup();
178     pmdChecksTabControl.cleanup();
179   }
180 
181   /**
182    * Override ButtonDialog
183    *
184    * @return the index of the button or UNINITIALIZED_VALUE
185    */
186   public int getVgap() {
187     return (0);
188   }
189 
190   private synchronized void jbInit() {
191     final int VIEWPORT_WIDTH = 500;
192     final int VIEWPORT_HEIGHT = 300;
193 
194     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
195     final String[] BUTTONS =
196     { RESOURCES.getString("BUTTON_005"), RESOURCES.getString("BUTTON_001") };
197 
198     setModal(true);
199     setResizable(true);
200     setTitle(RESOURCES.getString("WINDOWTITLE_024"));
201     setButtons(BUTTONS);
202     setInitialButton(OK);
203 
204     final JTextArea textArea = getGeneralInformation();
205     final int TEXTAREA_WIDTH = 500;
206     final int TEXTAREA_HEIGHT =
207       textArea.getLineCount() * (textArea.getFontMetrics(getFont()).getHeight());
208     textArea.setPreferredSize(new Dimension(TEXTAREA_WIDTH, TEXTAREA_HEIGHT));
209 
210     final JTabbedPane tabs = new JTabbedPane();
211     checksTabControl = new QStudioCheckPropertiesControl();
212     checksTabControl.setEnabled(false);
213     checksTabControl.setViewportSize(
214       new Dimension(VIEWPORT_WIDTH, VIEWPORT_HEIGHT)
215     );
216     checksTabControl.setPreferredSize(
217       new Dimension(VIEWPORT_WIDTH, VIEWPORT_HEIGHT)
218     );
219 
220     tabs.add(RESOURCES.getString("LABEL_031"), checksTabControl);
221 
222     Utilities.discardResult(panel.add(textArea));
223     Utilities.discardResult(panel.add(tabs));
224 
225     this.setContent(panel);
226   }
227 
228   /**
229    * - override superclass reaction to WindowClose event
230    */
231   protected void cancelDialog() {
232     checksTabControl.cleanup();
233     super.cancelDialog();
234   }
235 
236   /**
237    * - override superclass button activation
238    *
239    * @param action indication whether "ok" or
240    *               "cancel" was selected.
241    */
242   protected void onActionPerformed(int action) {
243     checksTabControl.cleanup();
244     super.onActionPerformed(action);
245   }
246 
247   private JTextArea getGeneralInformation() {
248     final int TEXTAREA_COLUMNS = 65;
249     final JTextArea text = new JTextArea();
250     final JLabel dummy = new JLabel("dummy");
251 
252     text.setFont(dummy.getFont());
253     text.setForeground(dummy.getForeground());
254     text.setEditable(false);
255     text.setLineWrap(false);
256     text.setWrapStyleWord(false);
257     text.setOpaque(false);
258     text.setColumns(TEXTAREA_COLUMNS);
259     setMessage(text);
260 
261     return (text);
262   }
263 
264   private synchronized void setMessage(JTextArea text) {
265     final String[] MESSAGE = getWarningMessage();
266 
267     if (MESSAGE != null) {
268       text.setText(MESSAGE[0]);
269 
270       for (int i = 1; i < MESSAGE.length; i++) {
271         text.append("\n" + MESSAGE[i]);
272       }
273     }
274 
275     text.setRows(MESSAGE.length);
276   }
277 
278   private String[] getWarningMessage() {
279     return (warningMessage);
280   }
281 
282   private synchronized void setWarningMessage(String[] message) {
283     warningMessage = message;
284   }
285 
286   private Dimension getMyPrefferedSize() {
287     final int PREFERRED_WIDTH = 550;
288     final int PREFERRED_HEIGHT = 500;
289 
290     return (new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
291   }
292 }
293