001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.installer.panel;
005:
006: import com.tc.installer.util.WarFileFilter;
007: import com.zerog.ia.api.pub.CustomCodePanel;
008: import com.zerog.ia.api.pub.CustomCodePanelProxy;
009:
010: import java.awt.Color;
011: import java.awt.GridBagConstraints;
012: import java.awt.GridBagLayout;
013: import java.awt.Insets;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.io.File;
017: import java.io.FileFilter;
018: import java.util.ArrayList;
019:
020: import javax.swing.BorderFactory;
021: import javax.swing.ButtonGroup;
022: import javax.swing.JButton;
023: import javax.swing.JFileChooser;
024: import javax.swing.JFrame;
025: import javax.swing.JList;
026: import javax.swing.JPanel;
027: import javax.swing.JRadioButton;
028: import javax.swing.JScrollPane;
029: import javax.swing.JTextField;
030: import javax.swing.JTextPane;
031:
032: public class ChooseWarFiles extends CustomCodePanel {
033:
034: private static final String MESSAGE_TEXT_NO = "I do not have any web applications that I would like to use with Terracotta. Use the only the default applications.";
035: private static final String MESSAGE_TEXT_YES = "Copy web applications (.war only) from the following directory:";
036: private static final String TITLE = "Select war Applications to Include in Sandbox";
037: private JList list;
038: private JButton button;
039: private JTextField field;
040: private CustomCodePanelProxy panelProxy;
041: private String startDir;
042:
043: public boolean setupUI(CustomCodePanelProxy customCodePanelProxy) {
044: panelProxy = customCodePanelProxy;
045: startDir = (String) panelProxy
046: .getVariable("lax.nl.env.CATALINA_HOME");
047: if (startDir == null)
048: startDir = panelProxy.getVariable("SYSTEM_DRIVE_ROOT")
049: .toString();
050:
051: JPanel p = new JPanel();
052: p.setOpaque(false);
053: p.setLayout(new GridBagLayout());
054: GridBagConstraints c = new GridBagConstraints();
055: c.fill = GridBagConstraints.BOTH;
056: c.insets = new Insets(1, 1, 16, 1);
057: c.anchor = GridBagConstraints.NORTHWEST;
058:
059: c.gridx = 0;
060: c.gridy = 0;
061: c.weightx = 1;
062: c.weighty = 0;
063: p.add(choicePanel(), c);
064:
065: c.gridx = 0;
066: c.gridy = 1;
067: c.weightx = 1;
068: c.weighty = 0;
069: p.add(browserPanel(), c);
070:
071: list = new JList();
072: list.setBackground(Color.WHITE);
073: JScrollPane scroll = new JScrollPane(list);
074:
075: c.gridy = 2;
076: c.weighty = 1;
077: c.insets = new Insets(1, 1, 1, 1);
078: p.add(scroll, c);
079: add(p);
080:
081: return true;
082: }
083:
084: private JPanel browserPanel() {
085: JPanel p = new JPanel();
086: p.setOpaque(false);
087: p.setLayout(new GridBagLayout());
088: GridBagConstraints c = new GridBagConstraints();
089: c.fill = GridBagConstraints.HORIZONTAL;
090: c.insets = new Insets(0, 0, 0, 0);
091: c.anchor = GridBagConstraints.CENTER;
092: c.gridx = 0;
093: c.gridy = 0;
094: c.weightx = 1;
095: c.weighty = 0;
096:
097: field = new JTextField();
098: field.setFocusable(false);
099:
100: button = new JButton("Choose...");
101: button.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: if (!field.getText().trim().equals("")) {
104: try {
105: new File(field.getText());
106: startDir = field.getText();
107: } catch (Exception ee) {
108: // not implemented
109: }
110: }
111: JFileChooser fileChooser = new JFileChooser(startDir);
112: fileChooser
113: .setDialogTitle("Browse for .war Application Directory");
114: fileChooser.setDragEnabled(false);
115: fileChooser
116: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
117: fileChooser.setMultiSelectionEnabled(false);
118:
119: int returnValue = fileChooser.showDialog(new JFrame(),
120: "OK");
121: if (returnValue == JFileChooser.APPROVE_OPTION) {
122: File warDir = fileChooser.getSelectedFile();
123: field.setText(warDir.getAbsolutePath());
124:
125: // populate list
126: FileFilter filter = new WarFileFilter();
127: File[] warList = new File(warDir.getAbsolutePath())
128: .listFiles(filter);
129: ArrayList wars = new ArrayList();
130: for (int i = 0; i < warList.length; i++) {
131: wars.add(warList[i].getName());
132: }
133:
134: // select items
135: list.setListData(wars.toArray());
136: int size = list.getModel().getSize();
137: int[] indices = new int[size];
138: for (int i = 0; i < size; i++) {
139: indices[i] = i;
140: }
141: list.setSelectedIndices(indices);
142: }
143: }
144: });
145: button.setFont(getFont());
146:
147: p.add(field, c);
148: c.gridx = 1;
149: c.weightx = 0;
150: c.insets = new Insets(0, 6, 0, 0);
151: p.add(button, c);
152:
153: return p;
154: }
155:
156: private JPanel choicePanel() {
157: JRadioButton yesWars = new JRadioButton();
158: yesWars.setBackground(Color.WHITE);
159: yesWars.setActionCommand("yesWars");
160: yesWars.setSelected(true);
161: yesWars.addActionListener(new ActionListener() {
162: public void actionPerformed(ActionEvent e) {
163: list.setEnabled(true);
164: button.setEnabled(true);
165: field.setEditable(true);
166: }
167: });
168: JRadioButton noWars = new JRadioButton();
169: noWars.setBackground(Color.WHITE);
170: noWars.setActionCommand("noWars");
171: noWars.setSelected(false);
172: noWars.addActionListener(new ActionListener() {
173: public void actionPerformed(ActionEvent e) {
174: list.setEnabled(false);
175: list.getSelectionModel().clearSelection();
176: button.setEnabled(false);
177: field.setEditable(false);
178: }
179: });
180:
181: ButtonGroup group = new ButtonGroup();
182: group.add(yesWars);
183: group.add(noWars);
184:
185: JTextPane msgYes = new JTextPane();
186: msgYes.setOpaque(false);
187: msgYes.setEditable(false);
188: msgYes.setFont(getFont());
189: msgYes.setText(MESSAGE_TEXT_YES);
190:
191: JTextPane msgNo = new JTextPane();
192: msgNo.setOpaque(false);
193: msgNo.setEditable(false);
194: msgNo.setFont(getFont());
195: msgNo.setText(MESSAGE_TEXT_NO + "\n");
196:
197: JPanel msgPanel = new JPanel();
198: msgPanel.setOpaque(false);
199: msgPanel.setBorder(BorderFactory.createCompoundBorder(
200: BorderFactory.createLineBorder(Color.BLACK),
201: BorderFactory.createEmptyBorder(3, 3, 3, 3)));
202: msgPanel.setLayout(new GridBagLayout());
203:
204: GridBagConstraints gbc = new GridBagConstraints();
205: gbc.fill = GridBagConstraints.HORIZONTAL;
206: gbc.insets = new Insets(4, 0, 0, 0);
207: gbc.anchor = GridBagConstraints.NORTHWEST;
208: gbc.gridx = 0;
209: gbc.gridy = 0;
210: gbc.weightx = 1;
211: gbc.weighty = 1;
212: msgPanel.add(noWars, gbc);
213: gbc.insets = new Insets(0, 0, 0, 0);
214: gbc.gridx = 1;
215: gbc.weightx = 100;
216: msgPanel.add(msgNo, gbc);
217: gbc.insets = new Insets(4, 0, 0, 0);
218: gbc.gridx = 0;
219: gbc.gridy = 1;
220: gbc.weightx = 1;
221: msgPanel.add(yesWars, gbc);
222: gbc.insets = new Insets(0, 0, 0, 0);
223: gbc.gridx = 1;
224: gbc.weightx = 100;
225: msgPanel.add(msgYes, gbc);
226: return msgPanel;
227: }
228:
229: public boolean okToContinue() {
230: Object[] wars = list.getSelectedValues();
231: for (int i = 0; i < wars.length; i++) {
232: panelProxy.setVariable("USR_CP_WAR_" + i, panelProxy
233: .substitute(wars[i].toString()));
234: }
235: return true;
236: }
237:
238: public String getTitle() {
239: return TITLE;
240: }
241: }
|