001: /*
002: * CommandLineOptionsPanel.java
003: *
004: * Created on 19 СентÑ?брь 2007 г., 14:13
005: */
006:
007: package org.netbeans.modules.php.project.options;
008:
009: import java.awt.BorderLayout;
010: import java.awt.Color;
011: import java.io.File;
012: import java.io.IOException;
013: import javax.swing.JComponent;
014: import javax.swing.JFileChooser;
015: import javax.swing.JPanel;
016: import javax.swing.event.DocumentEvent;
017: import javax.swing.event.DocumentListener;
018: import org.netbeans.api.progress.ProgressHandle;
019: import org.netbeans.api.progress.ProgressHandleFactory;
020: import org.netbeans.modules.php.project.customizer.CustomizerCommandLine;
021: import org.netbeans.modules.php.project.ui.actions.SystemPackageFinder;
022: import org.netbeans.modules.php.project.wizards.NewPhpProjectWizardIterator;
023: import org.openide.util.NbBundle;
024:
025: /**
026: *
027: * @author avk
028: */
029: public class CommandLineOptionsPanel extends JPanel {
030:
031: private static final String SEARCH = "SEARCH"; // NOI18N
032: private static final String BROWSE = "BROWSE"; // NOI18N
033: private static final String SELECT_PHP_LOCATION = "LBL_SelectPhpLocation"; // NOI18N
034: private static final String MSG_ILLEGAL_PHP_PATH = "MSG_IllegalPhpPath"; // NOI18N
035: private static final String MSG_ABSENT_FILE = "MSG_AbsentFile"; // NOI18N
036: private static final String MSG_NO_SEARCH_RESULTS = "MSG_NoSearchResults"; // NOI18N
037:
038: /** Creates new form CommandLineOptionsPanel */
039: public CommandLineOptionsPanel(
040: CommandLineOptionsPanelController controller) {
041: myControler = controller;
042: initComponents();
043: addListeners();
044: }
045:
046: void load() {
047: myLoaded = false;
048:
049: String path = CommandLinePreferences.getInstance()
050: .getPhpInterpreter();
051: if (path != null) {
052: myInterpreterPath.setText(path);
053: }
054: myLoaded = true;
055: }
056:
057: void store() {
058: interpreterPathIsValid();
059: CommandLinePreferences.getInstance().setPhpInterpreter(
060: myInterpreterPath.getText());
061: }
062:
063: void cancel() {
064: // nothing to do
065: }
066:
067: private void addListeners() {
068: myInterpreterPath.getDocument().addDocumentListener(
069: new PathListener());
070: }
071:
072: private class PathListener implements DocumentListener {
073:
074: /* (non-Javadoc)
075: * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
076: */
077: public void changedUpdate(DocumentEvent e) {
078: fireChange();
079: }
080:
081: /* (non-Javadoc)
082: * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
083: */
084: public void insertUpdate(DocumentEvent e) {
085: fireChange();
086: }
087:
088: /* (non-Javadoc)
089: * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
090: */
091: public void removeUpdate(DocumentEvent e) {
092: fireChange();
093: }
094:
095: private void fireChange() {
096: interpreterPathIsValid();
097: myControler.changed();
098: }
099: }
100:
101: boolean interpreterPathIsValid() {
102: File destFolder = new File(myInterpreterPath.getText())
103: .getAbsoluteFile();
104: File file = getCanonicalFile(destFolder);
105: if (file == null) {
106: String message = NbBundle.getMessage(this .getClass(),
107: MSG_ABSENT_FILE);
108: //setErrorMessage(message);
109: setMessage(message);
110: return false;
111: }
112: if (!file.isFile()) {
113: String message = NbBundle.getMessage(this .getClass(),
114: MSG_ILLEGAL_PHP_PATH);
115: //setErrorMessage(message);
116: setMessage(message);
117: return false;
118: }
119: setMessage("");
120: return true;
121: }
122:
123: public static File getCanonicalFile(File file) {
124: try {
125: return file.getCanonicalFile();
126: } catch (IOException e) {
127: return null;
128: }
129: }
130:
131: private void configureProgressPanel(JComponent progressComponent) {
132: if (myProgress != null) {
133: myProgressContainer.remove(myProgress);
134: }
135:
136: myProgress = progressComponent == null ? new JPanel()
137: : progressComponent;
138: myProgressContainer.add(myProgress, BorderLayout.CENTER);
139:
140: myProgressContainer.validate();
141: validate();
142: }
143:
144: /** This method is called from within the constructor to
145: * initialize the form.
146: * WARNING: Do NOT modify this code. The content of this method is
147: * always regenerated by the Form Editor.
148: */
149: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
150: private void initComponents() {
151: java.awt.GridBagConstraints gridBagConstraints;
152:
153: interpreterContainer = new javax.swing.JPanel();
154: myPaneLabel = new javax.swing.JLabel();
155: myInterpreterLbl = new javax.swing.JLabel();
156: myInterpreterPath = new javax.swing.JTextField();
157: myProgressContainer = new javax.swing.JPanel();
158: myPhpProgramBrowse = new javax.swing.JButton();
159: myPhpProgramSearch = new javax.swing.JButton();
160: jSeparator1 = new javax.swing.JSeparator();
161: myMessageContainer = new javax.swing.JPanel();
162: myMessagePanel = new javax.swing.JTextPane();
163: spacer = new javax.swing.JPanel();
164:
165: setLayout(new java.awt.GridBagLayout());
166:
167: myPaneLabel.setText(org.openide.util.NbBundle.getMessage(
168: CommandLineOptionsPanel.class, "LBL_Pane_Label")); // NOI18N
169:
170: org.openide.awt.Mnemonics.setLocalizedText(myInterpreterLbl,
171: org.openide.util.NbBundle.getMessage(
172: CommandLineOptionsPanel.class,
173: "LBL_CommandPath")); // NOI18N
174:
175: myProgressContainer.setLayout(new java.awt.BorderLayout());
176:
177: org.openide.awt.Mnemonics.setLocalizedText(myPhpProgramBrowse,
178: org.openide.util.NbBundle.getMessage(
179: CommandLineOptionsPanel.class,
180: "LBL_BrowseLocation_Button")); // NOI18N
181: myPhpProgramBrowse.setActionCommand(BROWSE);
182: myPhpProgramBrowse
183: .addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(
185: java.awt.event.ActionEvent evt) {
186: myPhpProgramBrowsebrowsePressed(evt);
187: }
188: });
189:
190: org.openide.awt.Mnemonics.setLocalizedText(myPhpProgramSearch,
191: org.openide.util.NbBundle.getMessage(
192: CommandLineOptionsPanel.class,
193: "LBL_SearchLocation_Button")); // NOI18N
194: myPhpProgramSearch.setActionCommand(SEARCH);
195: myPhpProgramSearch
196: .addActionListener(new java.awt.event.ActionListener() {
197: public void actionPerformed(
198: java.awt.event.ActionEvent evt) {
199: myPhpProgramSearchActionPerformed(evt);
200: }
201: });
202:
203: org.jdesktop.layout.GroupLayout interpreterContainerLayout = new org.jdesktop.layout.GroupLayout(
204: interpreterContainer);
205: interpreterContainer.setLayout(interpreterContainerLayout);
206: interpreterContainerLayout
207: .setHorizontalGroup(interpreterContainerLayout
208: .createParallelGroup(
209: org.jdesktop.layout.GroupLayout.LEADING)
210: .add(
211: interpreterContainerLayout
212: .createSequentialGroup()
213: .addContainerGap()
214: .add(
215: interpreterContainerLayout
216: .createParallelGroup(
217: org.jdesktop.layout.GroupLayout.LEADING)
218: .add(
219: interpreterContainerLayout
220: .createSequentialGroup()
221: .add(
222: interpreterContainerLayout
223: .createParallelGroup(
224: org.jdesktop.layout.GroupLayout.TRAILING)
225: .add(
226: interpreterContainerLayout
227: .createSequentialGroup()
228: .add(
229: myInterpreterLbl,
230: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
231: 84,
232: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
233: .addPreferredGap(
234: org.jdesktop.layout.LayoutStyle.RELATED)
235: .add(
236: myInterpreterPath,
237: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
238: 250,
239: Short.MAX_VALUE))
240: .add(
241: myProgressContainer,
242: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
243: 338,
244: Short.MAX_VALUE))
245: .add(
246: 10,
247: 10,
248: 10)
249: .add(
250: interpreterContainerLayout
251: .createParallelGroup(
252: org.jdesktop.layout.GroupLayout.TRAILING,
253: false)
254: .add(
255: myPhpProgramSearch,
256: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
257: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
258: Short.MAX_VALUE)
259: .add(
260: myPhpProgramBrowse,
261: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
262: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
263: Short.MAX_VALUE)))
264: .add(
265: interpreterContainerLayout
266: .createSequentialGroup()
267: .add(
268: myPaneLabel)
269: .addPreferredGap(
270: org.jdesktop.layout.LayoutStyle.UNRELATED)
271: .add(
272: jSeparator1,
273: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
274: 188,
275: Short.MAX_VALUE)))
276: .addContainerGap()));
277: interpreterContainerLayout
278: .setVerticalGroup(interpreterContainerLayout
279: .createParallelGroup(
280: org.jdesktop.layout.GroupLayout.LEADING)
281: .add(
282: interpreterContainerLayout
283: .createSequentialGroup()
284: .addContainerGap()
285: .add(
286: interpreterContainerLayout
287: .createParallelGroup(
288: org.jdesktop.layout.GroupLayout.TRAILING)
289: .add(
290: myPaneLabel)
291: .add(
292: jSeparator1,
293: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
294: 10,
295: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
296: .addPreferredGap(
297: org.jdesktop.layout.LayoutStyle.RELATED)
298: .add(
299: interpreterContainerLayout
300: .createParallelGroup(
301: org.jdesktop.layout.GroupLayout.BASELINE)
302: .add(
303: myPhpProgramBrowse)
304: .add(
305: myInterpreterPath,
306: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
307: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
308: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
309: .add(
310: myInterpreterLbl))
311: .addPreferredGap(
312: org.jdesktop.layout.LayoutStyle.RELATED)
313: .add(
314: interpreterContainerLayout
315: .createParallelGroup(
316: org.jdesktop.layout.GroupLayout.LEADING)
317: .add(
318: myProgressContainer,
319: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
320: 23,
321: Short.MAX_VALUE)
322: .add(
323: myPhpProgramSearch))
324: .addContainerGap()));
325:
326: gridBagConstraints = new java.awt.GridBagConstraints();
327: gridBagConstraints.gridx = 0;
328: gridBagConstraints.gridy = 0;
329: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
330: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
331: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
332: gridBagConstraints.weightx = 1.0;
333: add(interpreterContainer, gridBagConstraints);
334:
335: myMessagePanel.setEditable(false);
336: myMessagePanel.setText(org.openide.util.NbBundle.getMessage(
337: CommandLineOptionsPanel.class,
338: "CommandLineOptionsPanel.myMessagePanel.text")); // NOI18N
339: myMessagePanel.setFocusable(false);
340: myMessagePanel.setMinimumSize(new java.awt.Dimension(6, 10));
341: myMessagePanel.setOpaque(false);
342: myMessagePanel.setPreferredSize(new java.awt.Dimension(0, 0));
343:
344: org.jdesktop.layout.GroupLayout myMessageContainerLayout = new org.jdesktop.layout.GroupLayout(
345: myMessageContainer);
346: myMessageContainer.setLayout(myMessageContainerLayout);
347: myMessageContainerLayout
348: .setHorizontalGroup(myMessageContainerLayout
349: .createParallelGroup(
350: org.jdesktop.layout.GroupLayout.LEADING)
351: .add(
352: org.jdesktop.layout.GroupLayout.TRAILING,
353: myMessageContainerLayout
354: .createSequentialGroup()
355: .addContainerGap()
356: .add(
357: myMessagePanel,
358: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
359: 439, Short.MAX_VALUE)));
360: myMessageContainerLayout
361: .setVerticalGroup(myMessageContainerLayout
362: .createParallelGroup(
363: org.jdesktop.layout.GroupLayout.LEADING)
364: .add(
365: myMessageContainerLayout
366: .createSequentialGroup()
367: .add(
368: myMessagePanel,
369: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
370: 24,
371: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
372: .addContainerGap(
373: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
374: Short.MAX_VALUE)));
375:
376: gridBagConstraints = new java.awt.GridBagConstraints();
377: gridBagConstraints.gridx = 0;
378: gridBagConstraints.gridy = 2;
379: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
380: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
381: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
382: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
383: gridBagConstraints.weightx = 1.0;
384: add(myMessageContainer, gridBagConstraints);
385:
386: org.jdesktop.layout.GroupLayout spacerLayout = new org.jdesktop.layout.GroupLayout(
387: spacer);
388: spacer.setLayout(spacerLayout);
389: spacerLayout.setHorizontalGroup(spacerLayout
390: .createParallelGroup(
391: org.jdesktop.layout.GroupLayout.LEADING).add(0,
392: 449, Short.MAX_VALUE));
393: spacerLayout.setVerticalGroup(spacerLayout.createParallelGroup(
394: org.jdesktop.layout.GroupLayout.LEADING).add(0, 171,
395: Short.MAX_VALUE));
396:
397: gridBagConstraints = new java.awt.GridBagConstraints();
398: gridBagConstraints.gridx = 0;
399: gridBagConstraints.gridy = 1;
400: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
401: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
402: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
403: gridBagConstraints.weightx = 1.0;
404: gridBagConstraints.weighty = 1.0;
405: add(spacer, gridBagConstraints);
406: }// </editor-fold>//GEN-END:initComponents
407:
408: private void myPhpProgramBrowsebrowsePressed(
409: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myPhpProgramBrowsebrowsePressed
410: if (BROWSE.equals(evt.getActionCommand())) {
411:
412: JFileChooser chooser = new JFileChooser();
413: chooser.setDialogTitle(NbBundle.getMessage(
414: CustomizerCommandLine.class, SELECT_PHP_LOCATION));
415: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
416: if (JFileChooser.APPROVE_OPTION == chooser
417: .showOpenDialog(this )) {
418: File projectDir = chooser.getSelectedFile();
419: myInterpreterPath.setText(projectDir.getAbsolutePath());
420: }
421: }
422: }//GEN-LAST:event_myPhpProgramBrowsebrowsePressed
423:
424: private void myPhpProgramSearchActionPerformed(
425: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myPhpProgramSearchActionPerformed
426: if (SEARCH.equals(evt.getActionCommand())) {
427:
428: //String title = NbBundle.getMessage(CustomizerCommandLine.class, "LBL_BTN_Perform_Auto");
429: ProgressHandle progress = ProgressHandleFactory
430: .createHandle(SEARCH); // NOI18N
431: JComponent progressComponent = ProgressHandleFactory
432: .createProgressComponent(progress);
433: configureProgressPanel(progressComponent);
434: myPhpProgramSearch.setEnabled(false);
435:
436: String interpreter = SystemPackageFinder
437: .getPhpInterpreterUserChoice(progress);
438: if (interpreter != null) {
439: myInterpreterPath.setText(interpreter);
440: } else {
441: String message = NbBundle.getMessage(this .getClass(),
442: MSG_NO_SEARCH_RESULTS);
443: setMessage(message);
444: }
445:
446: myPhpProgramSearch.setEnabled(true);
447: configureProgressPanel(null);
448: }
449: }//GEN-LAST:event_myPhpProgramSearchActionPerformed
450:
451: // Variables declaration - do not modify//GEN-BEGIN:variables
452: private javax.swing.JPanel interpreterContainer;
453: private javax.swing.JSeparator jSeparator1;
454: private javax.swing.JLabel myInterpreterLbl;
455: private javax.swing.JTextField myInterpreterPath;
456: private javax.swing.JPanel myMessageContainer;
457: private javax.swing.JTextPane myMessagePanel;
458: private javax.swing.JLabel myPaneLabel;
459: private javax.swing.JButton myPhpProgramBrowse;
460: private javax.swing.JButton myPhpProgramSearch;
461: private javax.swing.JPanel myProgressContainer;
462: private javax.swing.JPanel spacer;
463:
464: // End of variables declaration//GEN-END:variables
465:
466: public void setMessage(String msg) {
467: myMessagePanel.setText(msg);
468: myMessagePanel.setForeground(Color.DARK_GRAY);
469: }
470:
471: public void setErrorMessage(String msg) {
472: myMessagePanel.setText(msg);
473: myMessagePanel.setForeground(Color.RED);
474: }
475:
476: private CommandLineOptionsPanelController myControler;
477: private boolean myLoaded = false;
478: private JComponent myProgress;
479: }
|