001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.php.rt.providers.impl.local.apache;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Component;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.io.File;
049: import java.io.IOException;
050: import java.text.MessageFormat;
051: import java.util.Collection;
052: import java.util.HashMap;
053: import java.util.Map;
054: import java.util.Map.Entry;
055: import java.util.logging.Logger;
056: import javax.swing.DefaultComboBoxModel;
057: import javax.swing.JComponent;
058: import javax.swing.JFileChooser;
059: import javax.swing.JPanel;
060: import javax.swing.JRadioButton;
061: import javax.swing.SwingUtilities;
062: import javax.swing.event.DocumentEvent;
063: import javax.swing.event.DocumentListener;
064: import org.netbeans.api.progress.ProgressHandle;
065: import org.netbeans.api.progress.ProgressHandleFactory;
066: import org.netbeans.modules.php.rt.WebServerRegistry;
067: import org.netbeans.modules.php.rt.providers.impl.local.LocalHostImpl;
068: import org.netbeans.modules.php.rt.providers.impl.local.LocalServerProvider;
069: import org.netbeans.modules.php.rt.providers.impl.local.LocalUiConfigProvider;
070: import org.netbeans.modules.php.rt.spi.providers.WebServerProvider;
071: import org.netbeans.modules.php.rt.ui.AddHostWizard;
072: import org.openide.util.NbBundle;
073:
074: /**
075: *
076: * @author ads
077: */
078: public class ServerChooserVisual extends JPanel {
079:
080: private static final long serialVersionUID = -3170968073330222259L;
081: private static final String SELECT_CONFIG_LOCATION = "LBL_Select_config_Location"; // NOI18N
082: private static final String WIZARD_PANEL_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
083: private static final String BROWSE = "BROWSE"; // NOI18N
084: private static final String MSG_EMPTY_CONFIG_FILE = "MSG_EmptyConfigFile"; // NOI18N
085: // DOTO change to illegal path to config file
086: private static final String MSG_ILLEGAL_CONFIG_FILE_LOCATION = "MSG_IllegalConfigFileLocation"; // NOI18N
087: private static final String MSG_FAILED_AUTOCONFIG = "MSG_FailedAutoConfig"; // NOI18N
088: private static final String MSG_AUTOCONF_NOT_PERFORMED = "MSG_AtoconfIsNotPerformed"; // NOI18N
089: private static final String MSG_NO_PHP_MODULE = "MSG_NoPhpModuleFound"; // NOI18N
090: private static final String MSG_TOO_MANY_PHP_MODULES = "MSG_TooManyPhpModulesFound"; // NOI18N
091: private static final String MSG_NO_PHP_SO = "MSG_NoPhpSoLibrary"; // NOI18N
092: private static final String MSG_EXTENSIONS = "MSG_Extensions"; // NOI18N
093: private static final String MSG_NO_PHP_EXTENSIONS = "MSG_NoPhpExtensions"; // NOI18N
094: private static final String MSG_HOST_ALREADY_EXISTS = "MSG_HostAlreadyConfigured"; // NOI18N
095: /** Apache conf folder */
096: private static final String CONF = "conf"; // NOI18N
097: /** Apache httpd.conf */
098: public static final String HTTPD_CONF = "httpd.conf"; // NOI18N
099: public static final String PATH_TO_HTTPD_CONF = File.separator
100: + CONF + File.separator + HTTPD_CONF;
101:
102: private static Logger LOGGER = Logger
103: .getLogger(ServerChooserVisual.class.getName());
104:
105: public ServerChooserVisual(ServerChooserPanel panel) {
106: initComponents();
107:
108: myPanel = panel;
109: setOsDependency();
110: myMap.put(myManualButton, new Component[] { myLabel1,
111: myLocation, myBrowse });
112: myMap.put(myAutoButton, new Component[] { myLabel2,
113: myLocationAuto, myAuto });
114:
115: RadioListener listener = new RadioListener();
116: myManualButton.addActionListener(listener);
117: myAutoButton.addActionListener(listener);
118:
119: myLocationAuto.addActionListener(new ActionListener() {
120:
121: public void actionPerformed(ActionEvent e) {
122: setHosts();
123: getPanel().stateChanged();
124: }
125: });
126: myHosts.addActionListener(new ActionListener() {
127:
128: public void actionPerformed(ActionEvent e) {
129: getPanel().stateChanged();
130: }
131: });
132: myLocation.getDocument().addDocumentListener(
133: new TextFieldListener());
134: }
135:
136: private void setOsDependency() {
137: if (getPanel().isSolaris()) {
138: myAutoButton.setSelected(true);
139: myAutoButton.doClick();
140: } else {
141: myManualButton.setSelected(true);
142: myManualButton.doClick();
143: myAutoButton.setEnabled(false);
144: myLabel2.setEnabled(false);
145: myLocationAuto.setEnabled(false);
146: myAuto.setEnabled(false);
147: }
148: }
149:
150: public void read(AddHostWizard wizard) {
151: myWizard = wizard;
152:
153: HttpdHost host = (HttpdHost) myWizard
154: .getProperty(ServerChooserPanel.HOST);
155: if (host != null) {
156: myHosts.getModel().setSelectedItem(host);
157: }
158: String path = (String) myWizard
159: .getProperty(ServerChooserPanel.CONFIG_LOCATION);
160:
161: JRadioButton configWay = (JRadioButton) myWizard
162: .getProperty(ServerChooserPanel.CONFIG_WAY);
163:
164: if (myAutoButton.equals(configWay)) {
165: if (path != null) {
166: myLocationAuto.setSelectedItem(path);
167: }
168: myAutoButton.doClick();
169: // focus can be gained only when component becomes visible.
170: SwingUtilities.invokeLater(new Runnable() {
171:
172: public void run() {
173: myAutoButton.requestFocusInWindow();
174: }
175: });
176: } else if (myManualButton.equals(configWay)) {
177: myManualButton.doClick();
178: }
179: if (path != null) {
180: myLocation.setText(path);
181: }
182: }
183:
184: public void store(AddHostWizard wizard) {
185: if (myAutoButton.isSelected()) {
186: myWizard.putProperty(ServerChooserPanel.CONFIG_WAY,
187: myAutoButton);
188: if (myLocationAuto.getSelectedItem() != null) {
189: myWizard.putProperty(
190: ServerChooserPanel.CONFIG_LOCATION,
191: myLocationAuto.getSelectedItem().toString());
192: }
193: } else if (myManualButton.isSelected()) {
194: myWizard.putProperty(ServerChooserPanel.CONFIG_WAY,
195: myManualButton);
196: myWizard.putProperty(ServerChooserPanel.CONFIG_LOCATION,
197: myLocation.getText());
198: }
199:
200: Object object = myHosts.getModel().getSelectedItem();
201: assert object instanceof HttpdHost || object == null;
202: HttpdHost host = (HttpdHost) object;
203: myWizard.putProperty(ServerChooserPanel.HOST, host);
204:
205: // set data to fill LocalWebServerPanelVisual form
206: if (host != null) {
207: LocalHostImpl configuredHost = (LocalHostImpl) wizard
208: .getProperty(LocalUiConfigProvider.HOST);
209: if (configuredHost == null) {
210:
211: WebServerProvider provider = wizard
212: .getCurrentProvider();
213: assert provider instanceof LocalServerProvider;
214:
215: configuredHost = new LocalHostImpl(host.toString(),
216: host.getName(), host.getPort(), "",
217: (LocalServerProvider) provider);
218: configuredHost.setProperty(LocalHostImpl.DOCUMENT_PATH,
219: host.getPath());
220:
221: wizard.putProperty(LocalUiConfigProvider.HOST,
222: configuredHost);
223: }
224: }
225: wizard.putProperty(LocalUiConfigProvider.WEB_CONFIGS_CACHE,
226: myKnownConfigs);
227: }
228:
229: public boolean isContentValid() {
230: boolean isValid = validatePlatform()
231: && validateConfiguredPhp(getHttpdConfig());
232:
233: //myHosts.setEnabled(isValid);
234:
235: if (isValid) {
236: isValid = validateChosenHost();
237: }
238: // if manual configuration is selected,
239: // allow next step even if this is not completed
240: //if (myManualButton.isSelected()) {
241: // return true;
242: //}
243: // allow net step any way
244: return true;
245: //return isValid;
246: }
247:
248: private boolean validateChosenHost() {
249: //Do not prohibit records with the same http server
250: /*
251: HttpdHost host = (HttpdHost) myHosts.getSelectedItem();
252: Collection<org.netbeans.modules.php.rt.spi.providers.Host> hosts
253: = WebServerRegistry.getInstance().getHosts();
254: if (hosts.contains(host)) {
255: setErrorMessage(MSG_HOST_ALREADY_EXISTS, host);
256: return false;
257: } else {
258: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
259: }
260: */
261: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
262: return true;
263: }
264:
265: private boolean validateConfiguredPhp(HttpdConfig config) {
266: String pathPhpFive = config.getPhpSOPath(HttpdConfig.VERSION_5);
267: String pathPhpFour = config.getPhpSOPath(HttpdConfig.VERSION_4);
268: if (pathPhpFive == null && pathPhpFour == null) {
269: setErrorMessage(MSG_NO_PHP_MODULE);
270: return false;
271: }
272:
273: if (pathPhpFive != null && pathPhpFour != null) {
274: setErrorMessage(MSG_TOO_MANY_PHP_MODULES);
275: return false;
276: }
277:
278: String path = pathPhpFive == null ? pathPhpFour : pathPhpFive;
279: File file = new File(path);
280: if (!file.exists()) {
281: setErrorMessage(MSG_NO_PHP_SO, path);
282: return false;
283: }
284:
285: Collection<String> collection = config.getPhpExtensions();
286: boolean phpExtensionFound = false;
287: StringBuilder builder = new StringBuilder();
288: for (String string : collection) {
289: builder.append(string);
290: builder.append(" ");
291: if (string.equals("." + HttpdConfig.PHP)) {
292: phpExtensionFound = true;
293: break;
294: }
295: }
296: if (!phpExtensionFound) {
297: if (!collection.isEmpty()) {
298: setErrorMessage(MSG_EXTENSIONS, builder.toString());
299: } else {
300: setErrorMessage(MSG_NO_PHP_EXTENSIONS);
301: return false;
302: }
303: }
304: return true;
305: }
306:
307: private void configureProgressPanel(JComponent progressComponent) {
308: if (myProgress != null) {
309: myProgressContainer.remove(myProgress);
310: }
311:
312: if (progressComponent != null) {
313: myProgressContainer.add(progressComponent,
314: BorderLayout.CENTER);
315: }
316: myProgress = progressComponent;
317: myProgressContainer.validate();
318: validate();
319:
320: }
321:
322: /** This method is called from within the constructor to
323: * initialize the form.
324: * WARNING: Do NOT modify this code. The content of this method is
325: * always regenerated by the Form Editor.
326: */
327: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
328: private void initComponents() {
329:
330: myButtonGroup = new javax.swing.ButtonGroup();
331: myManualButton = new javax.swing.JRadioButton();
332: myAutoButton = new javax.swing.JRadioButton();
333: myLocation = new javax.swing.JTextField();
334: myLocationAuto = new javax.swing.JComboBox();
335: myBrowse = new javax.swing.JButton();
336: myAuto = new javax.swing.JButton();
337: myLabel1 = new javax.swing.JLabel();
338: myLabel2 = new javax.swing.JLabel();
339: jSeparator1 = new javax.swing.JSeparator();
340: myChoose = new javax.swing.JLabel();
341: myHosts = new javax.swing.JComboBox();
342: myProgressContainer = new javax.swing.JPanel();
343:
344: setName(org.openide.util.NbBundle.getBundle(
345: ServerChooserVisual.class)
346: .getString("LBL_ChooseServer")); // NOI18N
347:
348: myButtonGroup.add(myManualButton);
349: java.util.ResourceBundle bundle = java.util.ResourceBundle
350: .getBundle("org/netbeans/modules/php/rt/providers/impl/local/apache/Bundle"); // NOI18N
351: org.openide.awt.Mnemonics.setLocalizedText(myManualButton,
352: bundle.getString("LBL_Manual")); // NOI18N
353: myManualButton.setBorder(javax.swing.BorderFactory
354: .createEmptyBorder(0, 0, 0, 0));
355: myManualButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
356:
357: myButtonGroup.add(myAutoButton);
358: org.openide.awt.Mnemonics.setLocalizedText(myAutoButton, bundle
359: .getString("LBL_Auto")); // NOI18N
360: myAutoButton.setBorder(javax.swing.BorderFactory
361: .createEmptyBorder(0, 0, 0, 0));
362: myAutoButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
363: myAutoButton
364: .addActionListener(new java.awt.event.ActionListener() {
365: public void actionPerformed(
366: java.awt.event.ActionEvent evt) {
367: myAutoButtonActionPerformed(evt);
368: }
369: });
370:
371: myLocation.setToolTipText(org.openide.util.NbBundle.getMessage(
372: ServerChooserVisual.class,
373: "LBL_Select_config_Location_TIP")); // NOI18N
374:
375: org.openide.awt.Mnemonics.setLocalizedText(myBrowse, bundle
376: .getString("LBL_Browse")); // NOI18N
377: myBrowse.setActionCommand(bundle.getString("BROWSE")); // NOI18N
378: myBrowse.addActionListener(new java.awt.event.ActionListener() {
379: public void actionPerformed(java.awt.event.ActionEvent evt) {
380: doBrowse(evt);
381: }
382: });
383:
384: org.openide.awt.Mnemonics.setLocalizedText(myAuto, bundle
385: .getString("LBL_BTN_Perform_Auto")); // NOI18N
386: myAuto.addActionListener(new java.awt.event.ActionListener() {
387: public void actionPerformed(java.awt.event.ActionEvent evt) {
388: doAutoConfigure(evt);
389: }
390: });
391:
392: myLabel1.setLabelFor(myLocation);
393: org.openide.awt.Mnemonics.setLocalizedText(myLabel1, bundle
394: .getString("LBL_Location")); // NOI18N
395:
396: myLabel2.setLabelFor(myLocationAuto);
397: org.openide.awt.Mnemonics.setLocalizedText(myLabel2, bundle
398: .getString("LBL_LocationAuto")); // NOI18N
399:
400: myChoose.setLabelFor(myHosts);
401: org.openide.awt.Mnemonics.setLocalizedText(myChoose, bundle
402: .getString("LBL_Host")); // NOI18N
403:
404: myProgressContainer.setLayout(new java.awt.BorderLayout());
405:
406: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
407: this );
408: this .setLayout(layout);
409: layout
410: .setHorizontalGroup(layout
411: .createParallelGroup(
412: org.jdesktop.layout.GroupLayout.LEADING)
413: .add(
414: org.jdesktop.layout.GroupLayout.TRAILING,
415: layout
416: .createSequentialGroup()
417: .add(
418: layout
419: .createParallelGroup(
420: org.jdesktop.layout.GroupLayout.TRAILING)
421: .add(
422: org.jdesktop.layout.GroupLayout.LEADING,
423: myProgressContainer,
424: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
425: 474,
426: Short.MAX_VALUE)
427: .add(
428: org.jdesktop.layout.GroupLayout.LEADING,
429: layout
430: .createSequentialGroup()
431: .addContainerGap()
432: .add(
433: layout
434: .createParallelGroup(
435: org.jdesktop.layout.GroupLayout.TRAILING)
436: .add(
437: layout
438: .createSequentialGroup()
439: .add(
440: myLabel2)
441: .add(
442: 61,
443: 61,
444: 61))
445: .add(
446: layout
447: .createSequentialGroup()
448: .add(
449: myLabel1)
450: .addPreferredGap(
451: org.jdesktop.layout.LayoutStyle.RELATED)))
452: .add(
453: layout
454: .createParallelGroup(
455: org.jdesktop.layout.GroupLayout.LEADING)
456: .add(
457: myLocationAuto,
458: 0,
459: 215,
460: Short.MAX_VALUE)
461: .add(
462: org.jdesktop.layout.GroupLayout.TRAILING,
463: myLocation,
464: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
465: 215,
466: Short.MAX_VALUE))
467: .add(
468: 18,
469: 18,
470: 18)
471: .add(
472: layout
473: .createParallelGroup(
474: org.jdesktop.layout.GroupLayout.TRAILING,
475: false)
476: .add(
477: myBrowse,
478: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
479: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
480: Short.MAX_VALUE)
481: .add(
482: myAuto,
483: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
484: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
485: Short.MAX_VALUE)))
486: .add(
487: org.jdesktop.layout.GroupLayout.LEADING,
488: myManualButton)
489: .add(
490: org.jdesktop.layout.GroupLayout.LEADING,
491: myAutoButton)
492: .add(
493: org.jdesktop.layout.GroupLayout.LEADING,
494: layout
495: .createSequentialGroup()
496: .add(
497: myChoose,
498: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
499: 111,
500: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
501: .add(
502: 34,
503: 34,
504: 34)
505: .add(
506: myHosts,
507: 0,
508: 329,
509: Short.MAX_VALUE))
510: .add(
511: jSeparator1,
512: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
513: 474,
514: Short.MAX_VALUE))
515: .addContainerGap()));
516: layout
517: .setVerticalGroup(layout
518: .createParallelGroup(
519: org.jdesktop.layout.GroupLayout.LEADING)
520: .add(
521: layout
522: .createSequentialGroup()
523: .addContainerGap()
524: .add(myManualButton)
525: .add(14, 14, 14)
526: .add(
527: layout
528: .createParallelGroup(
529: org.jdesktop.layout.GroupLayout.BASELINE)
530: .add(myLabel1)
531: .add(myBrowse)
532: .add(
533: myLocation,
534: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
535: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
536: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
537: .add(18, 18, 18)
538: .add(myAutoButton)
539: .add(15, 15, 15)
540: .add(
541: layout
542: .createParallelGroup(
543: org.jdesktop.layout.GroupLayout.BASELINE)
544: .add(myLabel2)
545: .add(myAuto)
546: .add(
547: myLocationAuto,
548: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
549: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
550: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
551: .add(21, 21, 21)
552: .add(
553: jSeparator1,
554: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
555: 10,
556: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
557: .addPreferredGap(
558: org.jdesktop.layout.LayoutStyle.RELATED)
559: .add(
560: layout
561: .createParallelGroup(
562: org.jdesktop.layout.GroupLayout.BASELINE)
563: .add(myChoose)
564: .add(
565: myHosts,
566: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
567: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
568: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
569: .addPreferredGap(
570: org.jdesktop.layout.LayoutStyle.UNRELATED)
571: .add(
572: myProgressContainer,
573: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
574: 23,
575: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
576: .addContainerGap(36,
577: Short.MAX_VALUE)));
578:
579: myManualButton.getAccessibleContext().setAccessibleName(
580: bundle.getString("A11_Manual")); // NOI18N
581: myAutoButton.getAccessibleContext().setAccessibleName(
582: bundle.getString("A11_Auto")); // NOI18N
583: myLocation.getAccessibleContext().setAccessibleDescription(
584: bundle.getString("A11_Location_Txt")); // NOI18N
585: myLocationAuto.getAccessibleContext().setAccessibleDescription(
586: bundle.getString("A11_Location_Combo")); // NOI18N
587: myBrowse.getAccessibleContext().setAccessibleName(
588: bundle.getString("A11_Browse")); // NOI18N
589: myAuto.getAccessibleContext().setAccessibleName(
590: bundle.getString("A11_Perform_Auto")); // NOI18N
591: myLabel2.getAccessibleContext().setAccessibleName(
592: bundle.getString("A11_Platform")); // NOI18N
593: myChoose.getAccessibleContext().setAccessibleName(
594: bundle.getString("A11_Choose_Host")); // NOI18N
595: myHosts.getAccessibleContext().setAccessibleDescription(
596: bundle.getString("A11_Host_Combo")); // NOI18N
597: }// </editor-fold>//GEN-END:initComponents
598:
599: private void doAutoConfigure(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doAutoConfigure
600: String title = NbBundle.getMessage(ServerChooserPanel.class,
601: "LBL_BTN_Perform_Auto");
602: ProgressHandle progress = ProgressHandleFactory
603: .createHandle(title); // NOI18N
604: JComponent progressComponent = ProgressHandleFactory
605: .createProgressComponent(progress);
606: configureProgressPanel(progressComponent);
607: progress.start();
608:
609: SolarisPackageFinder finder = new SolarisPackageFinder();
610: String[] locations = finder.getPlatformLocations();
611: if (locations == null || locations.length == 0) {
612: setErrorMessage(MSG_FAILED_AUTOCONFIG);
613: } else {
614: myLocationAuto
615: .setModel(new DefaultComboBoxModel(locations));
616: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
617: setHosts();
618: }
619: getPanel().stateChanged();
620:
621: progress.finish();
622: configureProgressPanel(null);
623: }//GEN-LAST:event_doAutoConfigure
624:
625: private void doBrowse(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doBrowse
626: String command = evt.getActionCommand();
627:
628: if (NbBundle.getMessage(ServerChooserPanel.class, BROWSE)
629: .equals(command)) {
630: JFileChooser chooser = new JFileChooser();
631: chooser.setDialogTitle(NbBundle.getMessage(
632: ServerChooserPanel.class, SELECT_CONFIG_LOCATION));
633: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
634: String path = myLocation.getText();
635: if (path.length() > 0) {
636: File f = new File(path);
637: if (f.exists()) {
638: chooser.setSelectedFile(f);
639: }
640: }
641: if (JFileChooser.APPROVE_OPTION == chooser
642: .showOpenDialog(this )) {
643: File configFile = chooser.getSelectedFile();
644: myLocation.setText(configFile.getAbsolutePath());
645: setHosts();
646: }
647: getPanel().stateChanged();
648: }
649: }//GEN-LAST:event_doBrowse
650:
651: private void myAutoButtonActionPerformed(
652: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myAutoButtonActionPerformed
653: // TODO add your handling code here:
654: }//GEN-LAST:event_myAutoButtonActionPerformed
655:
656: private boolean validatePlatform() {
657: if (myManualButton.isSelected()) {
658: return validateCustomPath();
659: } else if (myAutoButton.isSelected()) {
660: return validateAutoConfig();
661: }
662: return false;
663: }
664:
665: private boolean validateAutoConfig() {
666: String message = "";
667: String str = (String) myWizard
668: .getProperty(WIZARD_PANEL_ERROR_MESSAGE);
669:
670: if (str != null && str.length() > 0) {
671: /*
672: * In the case when autoconfig button was pressed and it already sets
673: * error message. We use it now for showing.
674: */
675: message = str;
676: } else if (myLocationAuto.getItemCount() == 0) {
677: // there was no autoconfig action yet
678: message = NbBundle.getMessage(ServerChooserVisual.class,
679: MSG_AUTOCONF_NOT_PERFORMED);
680: }
681: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message);
682: if (message.length() == 0) {
683: String path = null;
684: if (myLocationAuto.getSelectedItem() != null) {
685: path = myLocationAuto.getSelectedItem().toString();
686: }
687: return validateLocation(path);
688: } else {
689: return false;
690: }
691: }
692:
693: private void setHosts() {
694: HttpdConfig config = getAndSetHttpdConfig();
695: HttpdHost[] hosts = config.getHosts();
696:
697: updateConfigsCache(hosts);
698: myHosts.setModel(new DefaultComboBoxModel(hosts));
699: }
700:
701: private void updateConfigsCache(HttpdHost[] hosts) {
702: for (HttpdHost host : hosts) {
703: String key = host.getName() + ":" + host.getPort();
704: if (!myKnownConfigs.containsKey(key)) {
705: myKnownConfigs.put(key, host.getPlarformPath());
706: }
707: }
708: }
709:
710: private HttpdConfig getHttpdConfig() {
711: if (myConfig == null) {
712: return getAndSetHttpdConfig();
713: } else {
714: return myConfig;
715: }
716: }
717:
718: private HttpdConfig getAndSetHttpdConfig() {
719: //String platformPath = null;
720: String configPath = null;
721: if (myManualButton.isSelected()) {
722: //platformPath = myLocation.getText();
723: //configPath = platformPath+PATH_TO_HTTPD_CONF;
724: // we now browse directly to config file.
725: configPath = myLocation.getText();
726: } else if (myAutoButton.isSelected()) {
727: Object obj = myLocationAuto.getSelectedItem();
728: if (obj != null) {
729: //platformPath = myLocationAuto.getSelectedItem().toString();
730: //configPath = platformPath+PATH_TO_HTTPD_CONF;
731: configPath = myLocationAuto.getSelectedItem()
732: .toString();
733: }
734: }
735: //myConfig = new HttpdConfig(platformPath, configPath);
736: myConfig = new HttpdConfig(configPath);
737: return myConfig;
738: }
739:
740: private boolean validateCustomPath() {
741: String location = myLocation.getText();
742:
743: return validateLocation(location);
744: }
745:
746: private boolean validateLocation(String location) {
747: if (location == null || location.trim().length() == 0) {
748: setErrorMessage(MSG_EMPTY_CONFIG_FILE);
749: return false;
750: }
751: File file = new File(location).getAbsoluteFile();
752: //String suggestedConf = file.getAbsolutePath();
753: //File confDir = new File(suggestedConf).getAbsoluteFile();
754: File canonicalFile = getCanonicalFile(file);
755: if (canonicalFile == null || !canonicalFile.exists()
756: || !canonicalFile.isFile()) {
757: setErrorMessage(MSG_ILLEGAL_CONFIG_FILE_LOCATION);
758: return false;
759: }
760:
761: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
762: return true;
763: }
764:
765: private ServerChooserPanel getPanel() {
766: return myPanel;
767: }
768:
769: private void setErrorMessage(String key, Object... args) {
770: String message = null;
771: if (args.length > 0) {
772: message = MessageFormat.format(NbBundle.getMessage(
773: ServerChooserVisual.class, key), args);
774: } else {
775: message = NbBundle.getMessage(ServerChooserVisual.class,
776: key);
777: }
778: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message);
779: }
780:
781: public static File getCanonicalFile(File file) {
782: try {
783: return file.getCanonicalFile();
784: } catch (IOException e) {
785: return null;
786: }
787: }
788:
789: private class RadioListener implements ActionListener {
790:
791: /*
792: * (non-Javadoc)
793: *
794: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
795: */
796: public void actionPerformed(ActionEvent event) {
797: Object obj = event.getSource();
798: for (Entry<JRadioButton, Component[]> entry : myMap
799: .entrySet()) {
800: JRadioButton button = entry.getKey();
801: Component[] components = entry.getValue();
802: for (Component component : components) {
803: component.setEnabled(button == obj);
804: }
805: }
806: if (myWizard != null) {
807: myWizard.putProperty(WIZARD_PANEL_ERROR_MESSAGE, "");
808: }
809: getPanel().stateChanged();
810: }
811: }
812:
813: private class TextFieldListener implements DocumentListener {
814:
815: public void changedUpdate(DocumentEvent e) {
816: actionPerformed();
817: }
818:
819: public void insertUpdate(DocumentEvent e) {
820: actionPerformed();
821: }
822:
823: public void removeUpdate(DocumentEvent e) {
824: actionPerformed();
825: }
826:
827: private void actionPerformed() {
828: setHosts();
829: getPanel().stateChanged();
830: }
831: }
832:
833: // Variables declaration - do not modify//GEN-BEGIN:variables
834: private javax.swing.JSeparator jSeparator1;
835: private javax.swing.JButton myAuto;
836: private javax.swing.JRadioButton myAutoButton;
837: private javax.swing.JButton myBrowse;
838: private javax.swing.ButtonGroup myButtonGroup;
839: private javax.swing.JLabel myChoose;
840: private javax.swing.JComboBox myHosts;
841: private javax.swing.JLabel myLabel1;
842: private javax.swing.JLabel myLabel2;
843: private javax.swing.JTextField myLocation;
844: private javax.swing.JComboBox myLocationAuto;
845: private javax.swing.JRadioButton myManualButton;
846: private javax.swing.JPanel myProgressContainer;
847: // End of variables declaration//GEN-END:variables
848: private ServerChooserPanel myPanel;
849: private Map<JRadioButton, Component[]> myMap = new HashMap<JRadioButton, Component[]>(
850: 2);
851: private AddHostWizard myWizard;
852: private HttpdConfig myConfig;
853: private JComponent myProgress;
854: private Map<String, String> myKnownConfigs = new HashMap<String, String>();
855: }
|