001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.project;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.io.File;
024: import java.net.MalformedURLException;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.LinkedList;
028:
029: import javax.swing.DefaultComboBoxModel;
030: import javax.swing.DefaultListModel;
031: import javax.swing.JList;
032: import javax.swing.JTable;
033: import javax.swing.table.TableColumnModel;
034:
035: import com.jeta.forms.components.panel.FormPanel;
036: import com.jeta.open.gui.framework.JETAPanel;
037: import com.jeta.swingbuilder.gui.beanmgr.BeanLoader;
038: import com.jeta.swingbuilder.gui.beanmgr.BeansModel;
039: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
040: import com.jeta.swingbuilder.store.ImportedBeanInfo;
041: import com.jeta.swingbuilder.store.ProjectLevelImportedBeansModel;
042: import com.jeta.swingbuilder.store.ProjectModel;
043:
044: /**
045: * Displays the view for editing the current project settings
046: *
047: * @author Jeff Tassin
048: */
049: public class ProjectSettingsView extends JETAPanel {
050: /**
051: * The projectSettings.jfrm form
052: */
053: private FormPanel m_view;
054:
055: /**
056: * The list model for the source path
057: */
058: private DefaultListModel m_list_model = new DefaultListModel();
059:
060: /**
061: * Set when we are editing an existing project
062: */
063: private ProjectModel m_existing_model;
064:
065: /**
066: * The beans model.
067: */
068: private BeansModel m_beansmodel;
069:
070: /**
071: * The list model for the classpaths
072: */
073: private DefaultListModel m_paths_model;
074:
075: /**
076: * Resonsible for loading beans from a classpath of URLs
077: */
078: private BeanLoader m_bean_loader;
079:
080: /**
081: * ctor
082: */
083: public ProjectSettingsView() {
084: initialize(null);
085: }
086:
087: /**
088: * ctor
089: */
090: public ProjectSettingsView(ProjectModel pmodel) {
091: m_existing_model = pmodel;
092: initialize(pmodel);
093: }
094:
095: public void addPath(String path) {
096: File rootDir = getProjectRootDir();
097: if (rootDir != null) {
098: path = PathParser.getRelativePath(rootDir, new File(path));
099: if (!m_list_model.contains(path)) {
100: m_list_model.addElement(path);
101: }
102: }
103: }
104:
105: /**
106: * Deletes the selected path from the view
107: */
108: public void deleteSelectedPath() {
109: JList list = m_view
110: .getList(ProjectSettingsNames.ID_PROJECT_DIRECTORY_LIST);
111: int index = list.getSelectedIndex();
112: if (index >= 0) {
113: m_list_model.removeElementAt(index);
114: }
115: }
116:
117: /**
118: * Adds a user to the list model
119: */
120: public void addBeanPath(File file, int mode) {
121: File rootDir = getProjectRootDir();
122: if (rootDir != null) {
123: File newFile = new File(file.getAbsolutePath());
124: String relFilePath = (mode == javax.swing.JFileChooser.DIRECTORIES_ONLY ? PathParser
125: .getRelativePath(rootDir, newFile)
126: : PathParser.getRelativeFile(rootDir, newFile));
127: if (!m_paths_model.contains(relFilePath)) {
128: try {
129: m_paths_model.addElement(relFilePath);
130: m_bean_loader = null;
131: } catch (Exception e) {
132: }
133: }
134: }
135: }
136:
137: /**
138: * Deletes the bean from the view/model
139: */
140: public void deleteSelectedBean() {
141: JTable table = m_view
142: .getTable(ProjectSettingsNames.ID_BEAN_TABLE);
143: int row = table.getSelectedRow();
144: m_beansmodel.removeRow(row);
145: table.repaint();
146: m_view.repaint();
147: }
148:
149: /**
150: * Deletes the selected URL from the view/model
151: */
152: public void deleteSelectedUrl() {
153: JList list = m_view
154: .getList(ProjectSettingsNames.ID_CLASSPATH_LIST);
155: int index = list.getSelectedIndex();
156: if (index >= 0) {
157: m_paths_model.removeElementAt(index);
158: }
159: m_bean_loader = null;
160: }
161:
162: /**
163: * @return the bean loader which is responsible for creating beans using the
164: * given classpaths
165: */
166: BeanLoader getBeanLoader() {
167: if (m_bean_loader == null) {
168: m_bean_loader = new BeanLoader();
169: for (int index = 0; index < m_paths_model.size(); index++) {
170: String relClasspath = (String) m_paths_model
171: .elementAt(index);
172: try {
173: File file = new File(getProjectRootDir(),
174: relClasspath);
175: m_bean_loader.addUrl(file.toURL());
176: } catch (MalformedURLException e) {
177: e.printStackTrace();
178: }
179: }
180: }
181: return m_bean_loader;
182: }
183:
184: /**
185: * @return the underlying beans model
186: */
187: BeansModel getBeansModel() {
188: return m_beansmodel;
189: }
190:
191: /**
192: * @return the selected bean
193: */
194: public ImportedBeanInfo getSelectedBean() {
195: JTable table = m_view
196: .getTable(ProjectSettingsNames.ID_BEAN_TABLE);
197: int row = table.getSelectedRow();
198: return m_beansmodel.getRow(row);
199: }
200:
201: /**
202: * @return the model that describes the imported beans and their classpaths
203: */
204: public ProjectLevelImportedBeansModel getProjectLevelImportedBeansModel() {
205: ProjectLevelImportedBeansModel plibm = new ProjectLevelImportedBeansModel(
206: (String) getSelectedItem(ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE));
207: if (m_beansmodel != null) {
208: for (int row = 0; row < m_beansmodel.getRowCount(); row++) {
209: plibm.addImportedBean(m_beansmodel.getRow(row));
210: }
211: }
212: if (m_paths_model != null) {
213: for (int index = 0; index < m_paths_model.size(); index++) {
214: plibm.addRelativeClasspath((String) m_paths_model
215: .elementAt(index));
216: }
217: }
218: return plibm;
219: }
220:
221: /**
222: * @return a project model defined by the information in this view.
223: */
224: public ProjectModel getModel() {
225: ProjectModel pmodel = new ProjectModel();
226: Collection paths = getPaths();
227: Iterator iter = paths.iterator();
228: while (iter.hasNext()) {
229: pmodel.addSourcePath((String) iter.next());
230: }
231:
232: pmodel
233: .setShared(m_view
234: .isSelected(ProjectSettingsNames.ID_PROJECT_FILE_SHARED));
235:
236: if (m_existing_model == null) {
237: pmodel.setProjectPath(getProjectFile());
238: } else {
239: pmodel.setProjectPath(m_existing_model.getProjectPath());
240: }
241:
242: pmodel
243: .setProjectEnvVariable((String) m_view
244: .getSelectedItem(ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE));
245:
246: pmodel
247: .setProjectLevelImportedBeans(getProjectLevelImportedBeansModel());
248:
249: pmodel.setClassPath(m_view
250: .getText(ProjectSettingsNames.ID_PROJECT_CLASSPATH));
251: return pmodel;
252:
253: }
254:
255: public File getProjectRootDir() {
256: String tempPath = null;
257:
258: if (isSelected(ProjectSettingsNames.ID_PROJECT_FILE_SHARED)) {
259: String envVar = (String) getSelectedItem(ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE);
260: if (envVar != null) {
261: String directory = System.getenv(envVar);
262: try {
263: File file = new File(directory);
264: tempPath = file.getAbsolutePath();
265: } catch (Exception e) {
266: }
267: }
268: } else {
269: if (m_existing_model == null) {
270: tempPath = getProjectFile();
271: } else {
272: tempPath = m_existing_model.getProjectPath();
273: }
274: File file = new File(tempPath);
275: if (!file.isDirectory()) {
276: tempPath = file.getParent();
277: }
278: }
279:
280: File rootDir = null;
281:
282: if ((tempPath != null) && (!"".equals(tempPath))) {
283: String path = FormDesignerUtils.fastTrim(tempPath);
284: char c = File.separatorChar;
285: if (c == '\\')
286: path = path.replace('/', File.separatorChar);
287: else
288: path = path.replace('\\', File.separatorChar);
289:
290: try {
291: rootDir = new File(path);
292: } catch (Exception e) {
293: rootDir = null;
294: }
295: }
296:
297: return rootDir;
298: }
299:
300: public String getProjectFile() {
301: String path = FormDesignerUtils
302: .fastTrim(getText(ProjectSettingsNames.ID_PROJECT_FILE_PATH));
303: char c = File.separatorChar;
304: if (c == '\\')
305: path = path.replace('/', File.separatorChar);
306: else
307: path = path.replace('\\', File.separatorChar);
308:
309: return path;
310: }
311:
312: /**
313: * @return a collection of source paths (String objects) in this view
314: */
315: public Collection getPaths() {
316: LinkedList list = new LinkedList();
317: for (int index = 0; index < m_list_model.size(); index++) {
318: list.add(m_list_model.elementAt(index));
319: }
320: return list;
321: }
322:
323: public void initialize(ProjectModel pmodel) {
324: setLayout(new BorderLayout());
325: m_view = new FormPanel(
326: "com/jeta/swingbuilder/gui/project/projectSettingsMain.jfrm");
327:
328: add(m_view, BorderLayout.CENTER);
329: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
330: 10, 10));
331:
332: m_view.getTabbedPane(
333: ProjectSettingsNames.ID_PROJECT_SETTINGS_TAB)
334: .setEnabledAt(
335: 1,
336: (pmodel != null && (pmodel.isShared()) ? true
337: : false));
338:
339: // Populate the JComboBox with Environment Variables...
340: m_view.getComboBox(
341: ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE)
342: .setModel(
343: new DefaultComboBoxModel(FormDesignerUtils
344: .getEnvVars(false)));
345:
346: m_view.getButton(ProjectSettingsNames.ID_PROJECT_FILE_BTN)
347: .setPreferredSize(new Dimension(24, 10));
348: m_view.getButton(ProjectSettingsNames.ID_PROJECT_CLASSPATH_BTN)
349: .setPreferredSize(new Dimension(24, 10));
350:
351: JList list = m_view
352: .getList(ProjectSettingsNames.ID_PROJECT_DIRECTORY_LIST);
353: list.setModel(m_list_model);
354:
355: JTable table = m_view
356: .getTable(ProjectSettingsNames.ID_BEAN_TABLE);
357: m_beansmodel = new BeansModel();
358: table.setModel(m_beansmodel);
359:
360: m_paths_model = new DefaultListModel();
361: JList classpathList = m_view
362: .getList(ProjectSettingsNames.ID_CLASSPATH_LIST);
363: classpathList.setModel(m_paths_model);
364:
365: if ((pmodel != null) && pmodel.isShared()) {
366: Iterator iterator = pmodel
367: .getProjectLevelImportedBeansModel()
368: .getImportedBeans().iterator();
369: while (iterator.hasNext()) {
370: ImportedBeanInfo beanInfo = (ImportedBeanInfo) ((ImportedBeanInfo) iterator
371: .next()).clone();
372: m_beansmodel.addRow(beanInfo);
373: }
374:
375: int col_width = 60;
376: TableColumnModel cmodel = table.getColumnModel();
377: cmodel.getColumn(BeansModel.ICON_COLUMN).setPreferredWidth(
378: col_width);
379: cmodel.getColumn(BeansModel.NAME_COLUMN).setPreferredWidth(
380: col_width * 5);
381: cmodel.getColumn(BeansModel.SCROLLABLE_COLUMN)
382: .setPreferredWidth(col_width);
383:
384: iterator = pmodel.getProjectLevelImportedBeansModel()
385: .getRelativeClasspaths().iterator();
386: while (iterator.hasNext()) {
387: m_paths_model.addElement((String) iterator.next());
388: }
389: }
390:
391: setController(new ProjectSettingsController(this ));
392:
393: m_view.enableComponent(
394: ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE,
395: false);
396: m_view.enableComponent(
397: ProjectSettingsNames.ID_PROJECT_ENV_VAR_REFRESH_BTN,
398: false);
399:
400: if (pmodel != null) {
401: m_view.enableComponent(
402: ProjectSettingsNames.ID_PROJECT_FILE_SHARED, false);
403: m_view.enableComponent(
404: ProjectSettingsNames.ID_PROJECT_FILE_PATH, false);
405: m_view.enableComponent(
406: ProjectSettingsNames.ID_PROJECT_FILE_BTN, false);
407:
408: m_view.setSelected(
409: ProjectSettingsNames.ID_PROJECT_FILE_SHARED, pmodel
410: .isShared());
411: m_view.getTabbedPane(
412: ProjectSettingsNames.ID_PROJECT_SETTINGS_TAB)
413: .setEnabledAt(1, pmodel.isShared());
414:
415: m_view.setSelectedItem(
416: ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE,
417: pmodel.getProjectEnvVariable());
418:
419: m_view.setText(ProjectSettingsNames.ID_PROJECT_FILE_PATH,
420: pmodel.getProjectPath());
421: Collection paths = pmodel.getSourcePaths();
422: Iterator iter = paths.iterator();
423: while (iter.hasNext()) {
424: m_list_model.addElement(iter.next());
425: }
426: m_view.setText(ProjectSettingsNames.ID_PROJECT_CLASSPATH,
427: pmodel.getClassPath());
428: } else {
429: m_view.setSelectedItem(
430: ProjectSettingsNames.ID_PROJECT_ROOT_ENV_VARIABLE,
431: null);
432: }
433: }
434: }
|