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.border;
020:
021: import java.awt.BorderLayout;
022: import java.util.Iterator;
023:
024: import javax.swing.DefaultListModel;
025: import javax.swing.JList;
026: import javax.swing.border.Border;
027:
028: import com.jeta.forms.components.panel.FormPanel;
029: import com.jeta.forms.store.properties.BorderProperty;
030: import com.jeta.forms.store.properties.CompoundBorderProperty;
031: import com.jeta.open.gui.framework.JETAPanel;
032:
033: /**
034: * View that is used to create and edit borders
035: *
036: * @author Jeff Tassin
037: */
038: public class CompoundBorderView extends JETAPanel {
039:
040: /**
041: * The actual view
042: */
043: private FormPanel m_view;
044:
045: /**
046: * The list model that handles the borders
047: */
048: private DefaultListModel m_borders_model;
049:
050: /**
051: * ctor
052: */
053: public CompoundBorderView(CompoundBorderProperty border) {
054: setLayout(new BorderLayout());
055: m_view = new FormPanel(
056: "com/jeta/swingbuilder/gui/border/compoundBorder.frm");
057: add(m_view, BorderLayout.CENTER);
058:
059: setController(new CompoundBorderController(this ));
060:
061: m_borders_model = new DefaultListModel();
062: m_view.getList(CompoundBorderNames.ID_BORDER_LIST).setModel(
063: m_borders_model);
064:
065: Iterator iter = border.iterator();
066: while (iter.hasNext()) {
067: BorderProperty bp = (BorderProperty) iter.next();
068: m_borders_model.addElement(bp);
069: }
070: updateBorderView();
071: }
072:
073: /**
074: * Adds a border to the list
075: */
076: public void addBorder(BorderProperty bp) {
077: m_borders_model.add(0, bp);
078: updateBorderView();
079: }
080:
081: /**
082: * Creates a Swing border
083: */
084: private Border createBorder() {
085: return createBorderProperty().createBorder(null);
086: }
087:
088: /**
089: * Creates a BorderProperty based on this view
090: */
091: public BorderProperty createBorderProperty() {
092: CompoundBorderProperty prop = new CompoundBorderProperty();
093: for (int index = 0; index < m_borders_model.size(); index++) {
094: BorderProperty bp = (BorderProperty) m_borders_model
095: .elementAt(index);
096: prop.addBorder(bp);
097: }
098: return prop;
099: }
100:
101: /**
102: * Ensure the selected border is visible in the list.
103: */
104: void ensureIndexIsVisible() {
105: JList list = m_view.getList(CompoundBorderNames.ID_BORDER_LIST);
106: int index = list.getSelectedIndex();
107: if (index >= 0)
108: list.ensureIndexIsVisible(index);
109: }
110:
111: /**
112: * @return the selected border in the border list.
113: */
114: public BorderProperty getSelectedBorder() {
115: return (BorderProperty) m_view.getList(
116: CompoundBorderNames.ID_BORDER_LIST).getSelectedValue();
117: }
118:
119: /**
120: * Modifies a given border and sets it to a new border
121: */
122: public void setBorder(BorderProperty new_border,
123: BorderProperty old_border) {
124: int index = m_borders_model.indexOf(old_border);
125: if (index >= 0) {
126: m_borders_model.set(index, new_border);
127: }
128: updateBorderView();
129: }
130:
131: /**
132: * Creates a swing border based on the properties in this view and displays
133: * it.
134: */
135: public void updateBorderView() {
136: Border b = createBorder();
137: m_view.getLabel(CompoundBorderNames.ID_BORDER_PREVIEW)
138: .setBorder(b);
139: }
140: }
|