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: package org.netbeans.modules.php.rt.providers.impl;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Color;
045: import java.awt.Dialog;
046: import java.awt.Window;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.awt.event.WindowAdapter;
050: import java.awt.event.WindowEvent;
051: import java.util.Properties;
052: import javax.swing.JPanel;
053: import org.netbeans.modules.php.rt.WebServerRegistry;
054: import org.netbeans.modules.php.rt.actions.AddHostAction;
055: import org.netbeans.modules.php.rt.providers.impl.AbstractProvider;
056: import org.netbeans.modules.php.rt.providers.impl.HostImpl;
057: import org.netbeans.modules.php.rt.spi.providers.WebServerProvider;
058: import org.netbeans.modules.php.rt.ui.AddHostWizard;
059: import org.openide.DialogDescriptor;
060: import org.openide.DialogDisplayer;
061: import org.openide.util.SharedClassObject;
062:
063: /**
064: *
065: * @author avk
066: */
067: public class DefaultServerCustomizer extends javax.swing.JPanel {
068:
069: /** Creates new form ServerCustomizer */
070: public DefaultServerCustomizer(HostImpl host) {
071: initComponents();
072: myHost = host;
073: myProperties = new Properties();
074:
075: }
076:
077: public Window createCustomizerDialog(
078: ServerCustomizerComponent innerPanel, String title) {
079: init(innerPanel);
080: OptionListener listener = new OptionListener();
081:
082: DialogDescriptor dialogDescriptor = new DialogDescriptor(this ,
083: title, true, DialogDescriptor.OK_CANCEL_OPTION,
084: DialogDescriptor.CANCEL_OPTION, listener);
085: Dialog dlg = DialogDisplayer.getDefault().createDialog(
086: dialogDescriptor);
087:
088: myDialog = dlg;
089: myDialogDescriptor = dialogDescriptor;
090:
091: return dlg;
092: }
093:
094: private void init(ServerCustomizerComponent inner) {
095:
096: if (myCustomizerPanel != null) {
097: myCustomizerContainer.remove(myCustomizerPanel);
098: }
099:
100: JPanel innerJPanel = inner.getPanel();
101: if (innerJPanel != null) {
102: myCustomizerContainer.add(BorderLayout.NORTH,
103: (JPanel) innerJPanel);
104: myCustomizerPanel = (JPanel) innerJPanel;
105: }
106:
107: myCustomizerContainer.validate();
108: validate();
109:
110: read(getHost());
111: }
112:
113: void read(HostImpl impl) {
114: if (myCustomizerPanel != null) {
115: if (myCustomizerPanel instanceof ServerCustomizerComponent) {
116: ServerCustomizerComponent panel = (ServerCustomizerComponent) myCustomizerPanel;
117:
118: getProperties().put(ServerCustomizerComponent.HOST,
119: impl);
120: panel.readValues(getProperties());
121: }
122: }
123: }
124:
125: HostImpl store() {
126: if (myCustomizerPanel != null) {
127: if (myCustomizerPanel instanceof ServerCustomizerComponent) {
128: ServerCustomizerComponent panel = (ServerCustomizerComponent) myCustomizerPanel;
129: panel.storeValues(getProperties());
130:
131: HostImpl host = (HostImpl) getProperties().get(
132: ServerCustomizerComponent.HOST);
133: WebServerProvider provider = (WebServerProvider) getProperties()
134: .get(ServerCustomizerComponent.PROVIDER);
135:
136: if (provider != null
137: && !provider.equals(host.getProvider())) {
138:
139: if (provider instanceof AbstractProvider) {
140: AbstractUiConfigProvider uiProvider = (AbstractUiConfigProvider) ((AbstractProvider) provider)
141: .getConfigProvider();
142: return uiProvider
143: .copyHost(host.getName(), host);
144: }
145: }
146:
147: return host;
148: }
149: }
150: return null;
151: }
152:
153: public void stateChanged() {
154: if (myCustomizerPanel != null) {
155: if (myCustomizerPanel instanceof ServerCustomizerComponent) {
156:
157: ServerCustomizerComponent panel = (ServerCustomizerComponent) myCustomizerPanel;
158:
159: setErrorMessage("");
160: boolean isContentValid = panel.doContentValidation();
161: if (myDialogDescriptor != null) {
162: if (isContentValid) {
163: myDialogDescriptor.setValid(true);
164: } else {
165: myDialogDescriptor.setValid(false);
166: }
167: }
168: }
169: }
170: }
171:
172: public void setMessage(String msg) {
173: myMessagePanel.setText(msg);
174: myMessagePanel.setForeground(Color.BLACK);
175: }
176:
177: public void setErrorMessage(String msg) {
178: myMessagePanel.setText(msg);
179: myMessagePanel.setForeground(Color.RED);
180: }
181:
182: /**
183: * Listens to the actions on the Customizer's option buttons
184: */
185: private class OptionListener extends WindowAdapter implements
186: ActionListener {
187:
188: OptionListener() {
189: }
190:
191: // Listening to OK button ----------------------------------------------
192: public void actionPerformed(ActionEvent e) {
193: if (e.getSource() == DialogDescriptor.OK_OPTION) {
194: // Store updated host
195: HostImpl host = store();
196: // TODO check that provider was changed. null is TMP
197: if (!host.getProvider().equals(myHost.getProvider())) {
198: AddHostAction action = AddHostAction.findInstance();
199: action.showCustomizer(host,
200: AddHostWizard.Mode.UPDATE_EXISTING);
201: } else {
202: instantiate(myHost, host);
203: }
204: }
205: // Close & dispose the the dialog
206: if (myDialog != null) {
207: myDialog.setVisible(false);
208: myDialog.dispose();
209: }
210: }
211:
212: private void instantiate(HostImpl oldHost, HostImpl newHost) {
213: AbstractProvider provider = (AbstractProvider) oldHost
214: .getProvider();
215:
216: provider.updateHost(oldHost, newHost);
217: // send notification
218: WebServerRegistry.getInstance().upadateHost(newHost);
219: }
220:
221: // Listening to window events ------------------------------------------
222: @Override
223: public void windowClosed(WindowEvent e) {
224: myDialog = null;
225: }
226:
227: @Override
228: public void windowClosing(WindowEvent e) {
229: //Dispose the dialog otherwsie the {@link WindowAdapter#windowClosed}
230: //may not be called
231: if (myDialog != null) {
232: myDialog.setVisible(false);
233: myDialog.dispose();
234: }
235: }
236: }
237:
238: /** This method is called from within the constructor to
239: * initialize the form.
240: * WARNING: Do NOT modify this code. The content of this method is
241: * always regenerated by the Form Editor.
242: */
243: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
244: private void initComponents() {
245: java.awt.GridBagConstraints gridBagConstraints;
246:
247: myCustomizerContainer = new javax.swing.JPanel();
248: myCustomizerPanel = new javax.swing.JPanel();
249: myMessageContainer = new javax.swing.JPanel();
250: myMessagePanel = new javax.swing.JTextPane();
251:
252: setLayout(new java.awt.GridBagLayout());
253:
254: myCustomizerContainer.setLayout(new java.awt.BorderLayout());
255:
256: org.jdesktop.layout.GroupLayout myCustomizerPanelLayout = new org.jdesktop.layout.GroupLayout(
257: myCustomizerPanel);
258: myCustomizerPanel.setLayout(myCustomizerPanelLayout);
259: myCustomizerPanelLayout
260: .setHorizontalGroup(myCustomizerPanelLayout
261: .createParallelGroup(
262: org.jdesktop.layout.GroupLayout.LEADING)
263: .add(0, 0, Short.MAX_VALUE));
264: myCustomizerPanelLayout
265: .setVerticalGroup(myCustomizerPanelLayout
266: .createParallelGroup(
267: org.jdesktop.layout.GroupLayout.LEADING)
268: .add(0, 0, Short.MAX_VALUE));
269:
270: myCustomizerContainer.add(myCustomizerPanel,
271: java.awt.BorderLayout.CENTER);
272:
273: gridBagConstraints = new java.awt.GridBagConstraints();
274: gridBagConstraints.gridx = 0;
275: gridBagConstraints.gridy = 0;
276: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
277: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
278: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
279: gridBagConstraints.weightx = 1.0;
280: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
281: add(myCustomizerContainer, gridBagConstraints);
282:
283: myMessagePanel.setEditable(false);
284: myMessagePanel.setFocusable(false);
285: myMessagePanel.setMinimumSize(new java.awt.Dimension(6, 10));
286: myMessagePanel.setOpaque(false);
287: myMessagePanel.setPreferredSize(new java.awt.Dimension(0, 0));
288:
289: org.jdesktop.layout.GroupLayout myMessageContainerLayout = new org.jdesktop.layout.GroupLayout(
290: myMessageContainer);
291: myMessageContainer.setLayout(myMessageContainerLayout);
292: myMessageContainerLayout
293: .setHorizontalGroup(myMessageContainerLayout
294: .createParallelGroup(
295: org.jdesktop.layout.GroupLayout.LEADING)
296: .add(
297: myMessageContainerLayout
298: .createSequentialGroup()
299: .add(
300: myMessagePanel,
301: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
302: 378, Short.MAX_VALUE)
303: .addContainerGap()));
304: myMessageContainerLayout
305: .setVerticalGroup(myMessageContainerLayout
306: .createParallelGroup(
307: org.jdesktop.layout.GroupLayout.LEADING)
308: .add(
309: myMessageContainerLayout
310: .createSequentialGroup()
311: .add(
312: myMessagePanel,
313: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
314: 22, Short.MAX_VALUE)
315: .addContainerGap()));
316:
317: gridBagConstraints = new java.awt.GridBagConstraints();
318: gridBagConstraints.gridx = 0;
319: gridBagConstraints.gridy = 1;
320: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
321: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
322: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
323: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
324: gridBagConstraints.weightx = 1.0;
325: gridBagConstraints.weighty = 1.0;
326: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
327: add(myMessageContainer, gridBagConstraints);
328: }// </editor-fold>//GEN-END:initComponents
329:
330: // Variables declaration - do not modify//GEN-BEGIN:variables
331: private javax.swing.JPanel myCustomizerContainer;
332: private javax.swing.JPanel myCustomizerPanel;
333: private javax.swing.JPanel myMessageContainer;
334: private javax.swing.JTextPane myMessagePanel;
335:
336: // End of variables declaration//GEN-END:variables
337: // End of variables declaration
338: // End of variables declaration
339: // End of variables declaration
340: protected HostImpl getHost() {
341: return myHost;
342: }
343:
344: protected Properties getProperties() {
345: return myProperties;
346: }
347:
348: private HostImpl myHost;
349: private Dialog myDialog;
350: private DialogDescriptor myDialogDescriptor;
351: private Properties myProperties;
352: }
|