KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > wizard > ServerInstallVisualPanel


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.connection.wizard;
18
19 import org.mc4j.console.connection.install.finder.ServerInstallInfo;
20 import org.mc4j.console.connection.install.finder.ServerInstallSelectorPanel;
21 import org.mc4j.console.connection.persistence.ServerInstallSetDatabase;
22 import org.openide.util.NbBundle;
23
24 import javax.swing.*;
25 import javax.swing.event.ListSelectionEvent JavaDoc;
26 import javax.swing.event.ListSelectionListener JavaDoc;
27 import java.awt.*;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.io.File JavaDoc;
31
32 /**
33  * This visual panel lets you select a server installation directory.
34  *
35  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
36  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
37  */

38 public class ServerInstallVisualPanel extends JPanel implements ListSelectionListener JavaDoc {
39
40     private static File JavaDoc displayDirectory;
41
42     private JButton addButton, removeButton;
43
44     private JList serverInstallList;
45     private DefaultListModel serverInstallModel;
46
47     /** The wizard panel descriptor associated with this GUI panel.
48      * If you need to fire state changes or something similar, you can
49      * use this handle to do so.
50      */

51     private final ServerInstallPanel panel;
52
53     /** Create the wizard panel and set up some basic properties. */
54     public ServerInstallVisualPanel(ServerInstallPanel panel) {
55         this.panel = panel;
56         initComponents();
57         setName(NbBundle.getMessage(ServerInstallVisualPanel.class, "TITLE_WizardPanel"));
58     }
59
60     private void initComponents() {
61
62         setLayout(new BorderLayout());
63
64         String JavaDoc message =
65             "Please select a server installation for " +
66             panel.getDescriptor().getSettings().getConnectionType().getDisplayName() +
67             ". If there are none available, press \"Add\" to the right and browse to the correct installation " +
68             "directory.";
69
70         JTextArea messageArea = new JTextArea(message);
71         messageArea.setEditable(false);
72         messageArea.setLineWrap(true);
73         messageArea.setOpaque(false);
74         messageArea.setFont(messageArea.getFont().deriveFont(Font.BOLD,14));
75         messageArea.setWrapStyleWord(true);
76         messageArea.setForeground(Color.darkGray);
77         messageArea.setMargin(new Insets(5,5,5,5));
78
79         add(messageArea, BorderLayout.NORTH);
80
81         serverInstallModel = new DefaultListModel();
82         serverInstallList = new JList(serverInstallModel);
83         serverInstallList.setCellRenderer(new DefaultListCellRenderer() {
84             public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
85                 Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
86                 ServerInstallInfo selectedInfo = (ServerInstallInfo) value;
87                 if (!panel.getDescriptor().getSettings().getConnectionType().getConnectionType().equals(selectedInfo.getServerConnectionType().getConnectionType())) {
88                     comp.setEnabled(false);
89                 }
90                 return comp;
91             }
92         });
93
94         serverInstallList.getSelectionModel().addListSelectionListener(this);
95
96         for (ServerInstallInfo serverInstallSettings : ServerInstallSetDatabase.getNodes()) {
97             serverInstallModel.addElement(serverInstallSettings);
98         }
99
100         JScrollPane listScrollPane = new JScrollPane(serverInstallList);
101         add(listScrollPane, BorderLayout.CENTER);
102
103         JPanel buttonPanel = new JPanel();
104         buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
105
106         addButton = new JButton("Add");
107         addButton.addActionListener(new ActionListener JavaDoc() {
108             public void actionPerformed(ActionEvent JavaDoc e) {
109                 addButtonActionPerform();
110             }
111         });
112
113         removeButton = new JButton("Remove");
114         removeButton.setEnabled(false);
115         removeButton.addActionListener(new ActionListener JavaDoc() {
116             public void actionPerformed(ActionEvent JavaDoc e) {
117                 removeButtonActionPerform();
118             }
119         });
120
121         buttonPanel.add(addButton);
122         buttonPanel.add(removeButton);
123
124         add(buttonPanel, BorderLayout.EAST);
125     }
126
127     public void valueChanged(ListSelectionEvent JavaDoc e) {
128         int selectionIndex = serverInstallList.getSelectedIndex();
129
130         removeButton.setEnabled(selectionIndex >=0);
131         panel.fireChangeEvent();
132     }
133
134
135     public void addButtonActionPerform() {
136
137         ServerInstallSelectorPanel selectorPanel = new ServerInstallSelectorPanel();
138         selectorPanel.setSize(400,500);
139         int x,y;
140         x = (Toolkit.getDefaultToolkit().getScreenSize().width / 2) -
141             (selectorPanel.getSize().width / 2);
142         y = (Toolkit.getDefaultToolkit().getScreenSize().height / 2) -
143             (selectorPanel.getSize().height / 2);
144
145         selectorPanel.setLocation(x,y);
146         selectorPanel.setModal(true);
147         selectorPanel.show();
148
149         ServerInstallInfo info = selectorPanel.getServerInstallSelectionInfo();
150
151         if (info != null) {
152             ServerInstallSetDatabase.addNode(info);
153             serverInstallModel.addElement(info);
154             serverInstallList.setSelectedIndex(serverInstallModel.getSize() -1);
155         }
156
157     }
158
159     public void removeButtonActionPerform() {
160         ServerInstallInfo info =
161             (ServerInstallInfo) serverInstallModel.remove(serverInstallList.getSelectedIndex());
162         ServerInstallSetDatabase.removeNode(info);
163     }
164
165     public ServerInstallInfo getServerInstallInfo() {
166         return (ServerInstallInfo) serverInstallList.getSelectedValue();
167     }
168
169 }
170
Popular Tags