001: /*
002: * WebServersExplorer.java
003: *
004: * Created on 8 Ð?оÑ?брь 2007 г., 12:46
005: */
006:
007: package org.netbeans.modules.php.rt.utils;
008:
009: import java.awt.Component;
010: import java.beans.PropertyChangeEvent;
011: import java.beans.PropertyChangeListener;
012: import java.beans.PropertyVetoException;
013: import java.util.Collection;
014: import java.util.logging.Level;
015: import java.util.logging.Logger;
016: import javax.swing.Action;
017: import javax.swing.tree.TreeSelectionModel;
018: import org.netbeans.modules.php.rt.WebServerRegistry;
019: import org.netbeans.modules.php.rt.WebServersRootNode;
020: import org.netbeans.modules.php.rt.actions.AddHostAction;
021: import org.netbeans.modules.php.rt.actions.CustomizeHostAction;
022: import org.netbeans.modules.php.rt.actions.DeleteAction;
023: import org.netbeans.modules.php.rt.spi.providers.Host;
024: import org.openide.DialogDescriptor;
025: import org.openide.DialogDisplayer;
026: import org.openide.NotifyDescriptor;
027: import org.openide.actions.CustomizeAction;
028: import org.openide.explorer.ExplorerManager;
029: import org.openide.explorer.ExplorerUtils;
030: import org.openide.explorer.view.BeanTreeView;
031: import org.openide.nodes.Children;
032: import org.openide.nodes.FilterNode;
033: import org.openide.nodes.Node;
034: import org.openide.nodes.NodeNotFoundException;
035: import org.openide.nodes.NodeOp;
036: import org.openide.util.NbBundle;
037: import org.openide.util.SharedClassObject;
038: import org.openide.windows.TopComponent;
039:
040: /**
041: *
042: * @author avk
043: */
044: public class WebServersExplorer extends TopComponent implements
045: ExplorerManager.Provider {
046: private static final String LBL_MANAGE_SERVERS_TITLE = "LBL_ManageServers_Title"; // NOI18N
047:
048: private static Logger LOGGER = Logger
049: .getLogger(WebServersExplorer.class.getName());
050:
051: /** Creates new form WebServersExplorer */
052: public WebServersExplorer() {
053: initComponents();
054: init();
055: }
056:
057: /*
058: public static WebServersExplorer findInstance() {
059: return SharedClassObject.findObject(WebServersExplorer.class, true);
060: }
061: */
062:
063: public boolean showDialog() {
064: boolean confirm = false;
065:
066: String title = NbBundle.getMessage(WebServersExplorer.class,
067: LBL_MANAGE_SERVERS_TITLE);
068:
069: DialogDescriptor dialog = new DialogDescriptor(this , title,
070: true, DialogDescriptor.OK_CANCEL_OPTION,
071: NotifyDescriptor.OK_OPTION, null);
072:
073: confirm = (DialogDisplayer.getDefault().notify(dialog) == NotifyDescriptor.OK_OPTION);
074: // just check that array has cell to store result
075: return confirm;
076: }
077:
078: public ExplorerManager getExplorerManager() {
079: return myExplManager;
080: }
081:
082: public boolean isRootSelected() {
083: Node node = getFirstSelectedNode();
084: if (node != null) {
085: return node.equals(myExplManager.getRootContext());
086: }
087: return false;
088: }
089:
090: public Host getSelection() {
091: Node node = getFirstSelectedNode();
092: if (node != null) {
093: return node.getLookup().lookup(Host.class);
094: }
095: return null;
096: }
097:
098: public void setSelection(Host host) {
099: if (host == null) {
100: return;
101: }
102: try {
103: Node root = myExplManager.getRootContext();
104: String[] path = new String[] { host.getId() };
105: Node node = NodeOp.findPath(root, path);
106: if (node != null) {
107: myExplManager.setSelectedNodes(new Node[] { node });
108: }
109: } catch (PropertyVetoException ex) {
110: LOGGER.log(Level.WARNING, null, ex);
111: } catch (NodeNotFoundException ex) {
112: // just do not select any node
113: //LOGGER.log(Level.WARNING, null, ex);
114: }
115: }
116:
117: /** Overriden to pass focus directly to main content, which in
118: * turn assures that some element is always selected
119: */
120: @Override
121: public boolean requestFocusInWindow() {
122: boolean result = super .requestFocusInWindow();
123: jScrollPane1.requestFocusInWindow();
124: return result;
125: }
126:
127: protected Node getFirstSelectedNode() {
128: Node[] nodes = myExplManager.getSelectedNodes();
129: if (nodes.length > 0 && nodes[0] != null) {
130: return nodes[0];
131: }
132: return null;
133: }
134:
135: protected void refreshButtonsEnablement() {
136: Node[] nodes = myExplManager.getSelectedNodes();
137: myCustomizeBtn.setEnabled(isCustomizeAvailable(nodes));
138: myDeleteBtn.setEnabled(isDeleteAvailable(nodes));
139: }
140:
141: private boolean hasDeleteAction(Node node) {
142: Action[] acts = node.getActions(false);
143: for (Action act : acts) {
144: if (act instanceof DeleteAction) {
145: return true;
146: }
147: }
148: return false;
149: }
150:
151: private boolean isCustomizeAvailable(Node[] nodes) {
152: if (nodes == null || nodes.length != 1) {
153: return false;
154: }
155: Node node = nodes[0];
156: if (node == null) {
157: return false;
158: }
159: return node.hasCustomizer();
160: }
161:
162: private boolean isDeleteAvailable(Node[] nodes) {
163: if (nodes == null || nodes.length != 1) {
164: return false;
165: }
166: for (Node node : nodes) {
167: if (node == null || !hasDeleteAction(node)) {
168: return false;
169: }
170: }
171: return true;
172: }
173:
174: private void init() {
175: associateLookup(ExplorerUtils.createLookup(myExplManager,
176: getActionMap()));
177:
178: Node root = new WebServersRootNode();
179: FilterNode fRoot = new ExplorerNode(root);
180: myExplManager.setRootContext(fRoot);
181:
182: TreeSelectionListener listener = new TreeSelectionListener();
183: myExplManager.addPropertyChangeListener(listener);
184:
185: //mgr.setRootContext(new AbstractNode(new MyChildren()));
186:
187: refreshButtonsEnablement();
188: startDefaultCreationWizard();
189: }
190:
191: private void startDefaultCreationWizard() {
192: Collection<Host> collection = WebServerRegistry.getInstance()
193: .getHosts();
194: if (collection.size() == 0) {
195: AddHostAction.findInstance().showCustomizer();
196: }
197: }
198:
199: private class ExplorerNode extends FilterNode {
200:
201: public ExplorerNode(Node node) {
202: this (node, new ExplorerNodeChildren(node));
203: }
204:
205: public ExplorerNode(Node node,
206: org.openide.nodes.Children children) {
207: super (node, children, node.getLookup());
208: }
209:
210: @Override
211: public Action[] getActions(boolean context) {
212: return super .getActions(context);
213: }
214:
215: @Override
216: public boolean hasCustomizer() {
217: LOGGER.info("<<<<<< " + getName() + " has customizer: "
218: + super .hasCustomizer());
219: return super .hasCustomizer();
220: }
221:
222: @Override
223: public Component getCustomizer() {
224: Component c = super .getCustomizer();
225: LOGGER.info("<<<<<< " + getName() + " customizer= " + c);
226: return c;
227: }
228:
229: }
230:
231: private class ExplorerNodeChildren extends FilterNode.Children {
232:
233: private static final int MAX_NODES_DEPTH = 3;
234:
235: ExplorerNodeChildren(final Node originalNode) {
236: super (originalNode);
237: }
238:
239: @Override
240: protected Node[] createNodes(Node originalNode) {
241: return super .createNodes(originalNode);
242: }
243:
244: @Override
245: protected Node copyNode(Node originalNode) {
246: String[] path = NodeOp.createPath(originalNode,
247: myExplManager.getRootContext());
248: if (path.length >= MAX_NODES_DEPTH) {
249: return new ExplorerNode(originalNode, Children.LEAF);
250: } else {
251: return new ExplorerNode(originalNode);
252: }
253: }
254:
255: }
256:
257: /** This method is called from within the constructor to
258: * initialize the form.
259: * WARNING: Do NOT modify this code. The content of this method is
260: * always regenerated by the Form Editor.
261: */
262: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
263: private void initComponents() {
264:
265: jPanel1 = new javax.swing.JPanel();
266: jScrollPane1 = new WebServersTreeView();
267: myAddNewBtn = new javax.swing.JButton();
268: myCustomizeBtn = new javax.swing.JButton();
269: myDeleteBtn = new javax.swing.JButton();
270:
271: jPanel1.setLayout(new java.awt.BorderLayout());
272: jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
273:
274: org.openide.awt.Mnemonics.setLocalizedText(myAddNewBtn,
275: org.openide.util.NbBundle.getMessage(
276: WebServersExplorer.class,
277: "LBL_WebServersExplorer_Add_Btn")); // NOI18N
278: myAddNewBtn
279: .addActionListener(new java.awt.event.ActionListener() {
280: public void actionPerformed(
281: java.awt.event.ActionEvent evt) {
282: myAddNewBtnActionPerformed(evt);
283: }
284: });
285:
286: org.openide.awt.Mnemonics.setLocalizedText(myCustomizeBtn,
287: org.openide.util.NbBundle.getMessage(
288: WebServersExplorer.class,
289: "LBL_WebServersExplorer_Customize_Btn")); // NOI18N
290: myCustomizeBtn
291: .addActionListener(new java.awt.event.ActionListener() {
292: public void actionPerformed(
293: java.awt.event.ActionEvent evt) {
294: myCustomizeBtnActionPerformed(evt);
295: }
296: });
297:
298: org.openide.awt.Mnemonics.setLocalizedText(myDeleteBtn,
299: org.openide.util.NbBundle.getMessage(
300: WebServersExplorer.class,
301: "LBL_WebServersExplorer_Delete_Btn")); // NOI18N
302: myDeleteBtn
303: .addActionListener(new java.awt.event.ActionListener() {
304: public void actionPerformed(
305: java.awt.event.ActionEvent evt) {
306: myDeleteBtnActionPerformed(evt);
307: }
308: });
309:
310: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
311: this );
312: this .setLayout(layout);
313: layout
314: .setHorizontalGroup(layout
315: .createParallelGroup(
316: org.jdesktop.layout.GroupLayout.LEADING)
317: .add(
318: org.jdesktop.layout.GroupLayout.TRAILING,
319: layout
320: .createSequentialGroup()
321: .addContainerGap()
322: .add(
323: jPanel1,
324: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
325: 326, Short.MAX_VALUE)
326: .addPreferredGap(
327: org.jdesktop.layout.LayoutStyle.UNRELATED)
328: .add(
329: layout
330: .createParallelGroup(
331: org.jdesktop.layout.GroupLayout.TRAILING,
332: false)
333: .add(
334: myDeleteBtn,
335: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
336: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
337: Short.MAX_VALUE)
338: .add(
339: myAddNewBtn,
340: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
341: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
342: Short.MAX_VALUE)
343: .add(
344: myCustomizeBtn))
345: .addContainerGap()));
346: layout
347: .setVerticalGroup(layout
348: .createParallelGroup(
349: org.jdesktop.layout.GroupLayout.LEADING)
350: .add(
351: layout
352: .createSequentialGroup()
353: .addContainerGap()
354: .add(
355: layout
356: .createParallelGroup(
357: org.jdesktop.layout.GroupLayout.LEADING)
358: .add(
359: jPanel1,
360: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
361: 236,
362: Short.MAX_VALUE)
363: .add(
364: layout
365: .createSequentialGroup()
366: .add(
367: myAddNewBtn)
368: .addPreferredGap(
369: org.jdesktop.layout.LayoutStyle.RELATED)
370: .add(
371: myDeleteBtn)
372: .add(
373: 24,
374: 24,
375: 24)
376: .add(
377: myCustomizeBtn)))
378: .addContainerGap(
379: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
380: Short.MAX_VALUE)));
381: }// </editor-fold>//GEN-END:initComponents
382:
383: private void myAddNewBtnActionPerformed(
384: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myAddNewBtnActionPerformed
385: AddHostAction.findInstance().showCustomizer();
386: }//GEN-LAST:event_myAddNewBtnActionPerformed
387:
388: private void myCustomizeBtnActionPerformed(
389: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myCustomizeBtnActionPerformed
390: Node[] nodes = myExplManager.getSelectedNodes();
391: CustomizeHostAction.findInstance().customize(nodes);
392: }//GEN-LAST:event_myCustomizeBtnActionPerformed
393:
394: private void myDeleteBtnActionPerformed(
395: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myDeleteBtnActionPerformed
396: Node[] nodes = myExplManager.getSelectedNodes();
397: DeleteAction.findInstance().delete(nodes);
398: }//GEN-LAST:event_myDeleteBtnActionPerformed
399:
400: // Variables declaration - do not modify//GEN-BEGIN:variables
401: private javax.swing.JPanel jPanel1;
402: private javax.swing.JScrollPane jScrollPane1;
403: private javax.swing.JButton myAddNewBtn;
404: private javax.swing.JButton myCustomizeBtn;
405: private javax.swing.JButton myDeleteBtn;
406:
407: // End of variables declaration//GEN-END:variables
408:
409: private class WebServersTreeView extends BeanTreeView {
410:
411: WebServersTreeView() {
412: setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
413: }
414:
415: @Override
416: protected boolean selectionAccept(Node[] nodes) {
417: if (nodes != null && nodes.length > 1) {
418: return false;
419: }
420: return true;
421: }
422:
423: @Override
424: public boolean requestFocusInWindow() {
425: return super .requestFocusInWindow();
426: }
427:
428: }
429:
430: private class TreeSelectionListener implements
431: PropertyChangeListener {
432:
433: public void propertyChange(PropertyChangeEvent evt) {
434: if (evt.getSource() != myExplManager) {
435: return;
436: }
437: String evtName = evt.getPropertyName();
438: if (ExplorerManager.PROP_SELECTED_NODES.equals(evtName)) {
439: selectionChanged();
440: return;
441: } else if (ExplorerManager.PROP_NODE_CHANGE.equals(evtName)) {
442:
443: }
444:
445: }
446:
447: private void selectionChanged() {
448: refreshButtonsEnablement();
449: }
450:
451: }
452:
453: private ExplorerManager myExplManager = new ExplorerManager();
454:
455: }
|