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.event.ActionEvent;
022: import java.awt.event.ActionListener;
023:
024: import javax.swing.DefaultListModel;
025: import javax.swing.JList;
026:
027: import com.jeta.forms.store.properties.BevelBorderProperty;
028: import com.jeta.forms.store.properties.BorderProperty;
029: import com.jeta.forms.store.properties.DefaultBorderProperty;
030: import com.jeta.forms.store.properties.EmptyBorderProperty;
031: import com.jeta.forms.store.properties.EtchedBorderProperty;
032: import com.jeta.forms.store.properties.LineBorderProperty;
033: import com.jeta.forms.store.properties.ShadowBorderProperty;
034: import com.jeta.forms.store.properties.TitledBorderProperty;
035: import com.jeta.open.gui.framework.JETAController;
036: import com.jeta.open.gui.framework.JETADialog;
037: import com.jeta.open.gui.utils.JETAToolbox;
038: import com.jeta.open.i18n.I18N;
039:
040: /**
041: * Controller for the CompoundBorderView
042: *
043: * @author Jeff Tassin
044: */
045: public class CompoundBorderController extends JETAController {
046: private CompoundBorderView m_view;
047:
048: /**
049: * ctor
050: */
051: public CompoundBorderController(CompoundBorderView view) {
052: super (view);
053: m_view = view;
054: assignAction(CompoundBorderNames.ID_DEFAULT_BORDER_BUTTON,
055: new AddDefaultBorderAction());
056: assignAction(CompoundBorderNames.ID_BEVEL_BORDER_BUTTON,
057: new AddBorderAction(BevelBorderView.class));
058: assignAction(CompoundBorderNames.ID_TITLED_BORDER_BUTTON,
059: new AddBorderAction(TitledBorderView.class));
060: assignAction(CompoundBorderNames.ID_ETCHED_BORDER_BUTTON,
061: new AddBorderAction(EtchedBorderView.class));
062: assignAction(CompoundBorderNames.ID_LINE_BORDER_BUTTON,
063: new AddBorderAction(LineBorderView.class));
064: assignAction(CompoundBorderNames.ID_EMPTY_BORDER_BUTTON,
065: new AddBorderAction(EmptyBorderView.class));
066: assignAction(CompoundBorderNames.ID_SHADOW_BORDER_BUTTON,
067: new AddBorderAction(ShadowBorderView.class));
068: assignAction(CompoundBorderNames.ID_EDIT_BORDER_BUTTON,
069: new EditBorderAction());
070:
071: assignAction(CompoundBorderNames.ID_DELETE_BORDER_BUTTON,
072: new DeleteBorderAction());
073: assignAction(CompoundBorderNames.ID_MOVE_UP_BUTTON,
074: new MoveUpAction());
075: assignAction(CompoundBorderNames.ID_MOVE_DOWN_BUTTON,
076: new MoveDownAction());
077: }
078:
079: /**
080: * Creates a view for editing the given border property
081: */
082: private AbstractBorderView createBorderView(BorderProperty bp) {
083: if (bp instanceof BevelBorderProperty) {
084: return new BevelBorderView();
085: } else if (bp instanceof EtchedBorderProperty) {
086: return new EtchedBorderView();
087: } else if (bp instanceof LineBorderProperty) {
088: return new LineBorderView();
089: } else if (bp instanceof EmptyBorderProperty) {
090: return new EmptyBorderView();
091: } else if (bp instanceof ShadowBorderProperty) {
092: return new ShadowBorderView();
093: } else if (bp instanceof TitledBorderProperty) {
094: return new TitledBorderView();
095: } else {
096: assert (false);
097: return null;
098: }
099: }
100:
101: /**
102: * Handler for creating a new BevelBorder
103: */
104: public class AddBorderAction implements ActionListener {
105: /**
106: * The border view class to create
107: */
108: private Class m_border_view_class;
109:
110: public AddBorderAction(Class borderViewClass) {
111: m_border_view_class = borderViewClass;
112: }
113:
114: public void actionPerformed(ActionEvent evt) {
115: try {
116: AbstractBorderView view = (AbstractBorderView) m_border_view_class
117: .newInstance();
118: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
119: JETADialog.class, m_view, true);
120: dlg.setPrimaryPanel(view);
121: dlg.setSize(dlg.getPreferredSize());
122: dlg.setTitle(I18N.getLocalizedMessage("Create Border"));
123: dlg.showCenter();
124: if (dlg.isOk()) {
125: BorderProperty bp = view.createBorderProperty();
126: m_view.addBorder(bp);
127: }
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131: }
132: }
133:
134: /**
135: * Handler for creating a new DefaultBorderProperty
136: */
137: public class AddDefaultBorderAction implements ActionListener {
138: public void actionPerformed(ActionEvent evt) {
139: m_view.addBorder(new DefaultBorderProperty());
140: }
141: }
142:
143: /**
144: * Deletes the selected border
145: */
146: public class DeleteBorderAction implements ActionListener {
147: public void actionPerformed(ActionEvent evt) {
148: JList list = m_view
149: .getList(CompoundBorderNames.ID_BORDER_LIST);
150: int index = list.getSelectedIndex();
151: if (index >= 0) {
152: DefaultListModel model = (DefaultListModel) list
153: .getModel();
154: model.removeElementAt(index);
155: index--;
156: if (index < 0)
157: index = 0;
158: if (model.size() > 0)
159: list.setSelectedIndex(index);
160: }
161: m_view.ensureIndexIsVisible();
162: }
163: }
164:
165: /**
166: * Edits the selected border
167: */
168: public class EditBorderAction implements ActionListener {
169: public void actionPerformed(ActionEvent evt) {
170: BorderProperty old_border = m_view.getSelectedBorder();
171: if (old_border != null
172: && !(old_border instanceof DefaultBorderProperty)) {
173: AbstractBorderView view = createBorderView(old_border);
174: view.setBorderProperty(old_border);
175: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
176: JETADialog.class, m_view, true);
177: dlg.setPrimaryPanel(view);
178: dlg.setSize(dlg.getPreferredSize());
179: dlg.setTitle(view.getDescription());
180: dlg.showCenter();
181: if (dlg.isOk()) {
182: BorderProperty new_border = view
183: .createBorderProperty();
184: m_view.setBorder(new_border, old_border);
185: }
186: }
187: }
188: }
189:
190: public class MoveUpAction implements ActionListener {
191: public void actionPerformed(ActionEvent evt) {
192: JList list = m_view
193: .getList(CompoundBorderNames.ID_BORDER_LIST);
194: DefaultListModel model = (DefaultListModel) list.getModel();
195: int index = list.getSelectedIndex();
196: if (index > 0 && model.size() > 1) {
197: Object mv_obj = model.getElementAt(index);
198: Object next_obj = model.getElementAt(index - 1);
199: model.setElementAt(mv_obj, index - 1);
200: model.setElementAt(next_obj, index);
201: list.setSelectedIndex(index - 1);
202: }
203: m_view.ensureIndexIsVisible();
204: }
205: }
206:
207: public class MoveDownAction implements ActionListener {
208: public void actionPerformed(ActionEvent evt) {
209: JList list = m_view
210: .getList(CompoundBorderNames.ID_BORDER_LIST);
211: DefaultListModel model = (DefaultListModel) list.getModel();
212: int index = list.getSelectedIndex();
213: if ((index + 1) < model.size()) {
214: Object mv_obj = model.getElementAt(index);
215: Object next_obj = model.getElementAt(index + 1);
216: model.setElementAt(mv_obj, index + 1);
217: model.setElementAt(next_obj, index);
218: list.setSelectedIndex(index + 1);
219: }
220: m_view.ensureIndexIsVisible();
221: }
222: }
223: }
|