001: package org.rapla.gui.internal.edit;
002:
003: import java.awt.BorderLayout;
004: import java.awt.CardLayout;
005: import java.awt.Color;
006: import java.awt.Dimension;
007: import java.awt.Graphics;
008: import java.awt.Rectangle;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.MouseAdapter;
012:
013: import javax.swing.BorderFactory;
014: import javax.swing.Box;
015: import javax.swing.BoxLayout;
016: import javax.swing.JComponent;
017: import javax.swing.JLabel;
018: import javax.swing.JList;
019: import javax.swing.JPanel;
020: import javax.swing.JScrollPane;
021: import javax.swing.ListModel;
022: import javax.swing.UIManager;
023: import javax.swing.border.EmptyBorder;
024: import javax.swing.event.ChangeEvent;
025: import javax.swing.event.ChangeListener;
026: import javax.swing.event.ListDataEvent;
027: import javax.swing.event.ListDataListener;
028: import javax.swing.event.ListSelectionEvent;
029: import javax.swing.event.ListSelectionListener;
030:
031: import org.rapla.components.calendar.NavButton;
032: import org.rapla.components.calendar.RaplaArrowButton;
033: import org.rapla.components.layout.TableLayout;
034: import org.rapla.components.xmlbundle.I18nBundle;
035: import org.rapla.framework.RaplaException;
036: import org.rapla.gui.toolkit.RaplaButton;
037: import org.rapla.gui.toolkit.RaplaColorList;
038: import org.rapla.gui.toolkit.RaplaWidget;
039:
040: final public class RaplaListEdit implements RaplaWidget {
041: boolean coloredBackground = false;
042: int oldIndex = -1;
043:
044: JPanel mainPanel = new JPanel();
045:
046: JLabel statusBar = new JLabel();
047: JPanel identifierPanel = new JPanel();
048: JLabel identifier = new JLabel();
049: JLabel nothingSelectedLabel = new JLabel();
050: JScrollPane scrollPane;
051:
052: NavButton prev = new NavButton('^');
053: NavButton next = new NavButton('v');
054:
055: RaplaArrowButton moveUpButton = new RaplaArrowButton('^', 25);
056: RaplaArrowButton moveDownButton = new RaplaArrowButton('v', 25);
057:
058: Color selectionBackground = UIManager
059: .getColor("List.selectionBackground");
060: Color background = UIManager.getColor("List.background");
061:
062: JPanel jointPanel = new JPanel() {
063: private static final long serialVersionUID = 1L;
064:
065: int xa[] = new int[4];
066: int ya[] = new int[4];
067:
068: public void paint(Graphics g) {
069: super .paint(g);
070: Dimension dim = getSize();
071: int index = list.getSelectedIndex();
072:
073: Rectangle rect = list.getCellBounds(index, index);
074: if (rect != null) {
075: int y = rect.y
076: - scrollPane.getViewport().getViewPosition().y;
077: int y1 = Math.min(dim.height, Math.max(0, y)
078: + scrollPane.getLocation().y);
079: int y2 = Math.min(dim.height, Math.max(0, y
080: + rect.height)
081: + scrollPane.getLocation().y);
082: xa[0] = 0;
083: ya[0] = y1;
084: xa[1] = dim.width;
085: ya[1] = 0;
086: xa[2] = dim.width;
087: ya[2] = dim.height;
088: xa[3] = 0;
089: ya[3] = y2;
090: g.setColor(selectionBackground);
091: g.fillPolygon(xa, ya, 4);
092: g.setColor(background);
093: g.drawLine(xa[0], ya[0], xa[1], ya[1]);
094: g.drawLine(xa[3], ya[3], xa[2], ya[2]);
095: }
096: }
097: };
098: JPanel content = new JPanel();
099: JPanel detailContainer = new JPanel();
100: JPanel editPanel = new JPanel();
101:
102: JList list = new JList() {
103: private static final long serialVersionUID = 1L;
104:
105: public void setModel(ListModel model) {
106: super .setModel(model);
107: model.addListDataListener(new ListDataListener() {
108: public void contentsChanged(ListDataEvent e) {
109: modelUpdate();
110: }
111:
112: public void intervalAdded(ListDataEvent e) {
113: }
114:
115: public void intervalRemoved(ListDataEvent e) {
116: }
117: });
118: }
119: };
120:
121: RaplaButton createNewButton = new RaplaButton();
122: RaplaButton removeButton = new RaplaButton();
123:
124: CardLayout cardLayout = new CardLayout();
125: private Listener listener = new Listener();
126: private ActionListener callback;
127:
128: public RaplaListEdit(I18nBundle i18n, JComponent detailContent,
129: ActionListener callback) throws RaplaException {
130: this .callback = callback;
131: JPanel toolbar = new JPanel();
132: toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
133: toolbar.add(createNewButton);
134: toolbar.add(removeButton);
135: toolbar.add(Box.createHorizontalStrut(5));
136: toolbar.add(moveUpButton);
137: toolbar.add(moveDownButton);
138: mainPanel.setLayout(new TableLayout(new double[][] {
139: { TableLayout.PREFERRED, TableLayout.PREFERRED,
140: TableLayout.FILL }, { TableLayout.FILL } }));
141: jointPanel.setPreferredSize(new Dimension(15, 50));
142: mainPanel.add(content, "0,0");
143: mainPanel.add(jointPanel, "1,0");
144: mainPanel.add(editPanel, "2,0");
145: editPanel.setLayout(cardLayout);
146: editPanel.add(nothingSelectedLabel, "0");
147: editPanel.add(detailContainer, "1");
148: content.setLayout(new BorderLayout());
149: content.add(toolbar, BorderLayout.NORTH);
150: scrollPane = new JScrollPane(list,
151: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
152: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
153: scrollPane.setPreferredSize(new Dimension(310, 80));
154: content.add(scrollPane, BorderLayout.CENTER);
155: content.add(statusBar, BorderLayout.SOUTH);
156: detailContainer.setLayout(new BorderLayout());
157: editPanel.setBorder(BorderFactory.createRaisedBevelBorder());
158: detailContainer.add(identifierPanel, BorderLayout.WEST);
159: detailContainer.add(detailContent, BorderLayout.CENTER);
160: //mainPanel.setBorder(new LineBorder(Color.black));
161:
162: identifierPanel.setLayout(new BorderLayout());
163: identifierPanel.add(prev, BorderLayout.NORTH);
164: identifierPanel.add(identifier, BorderLayout.CENTER);
165: identifierPanel.add(next, BorderLayout.SOUTH);
166: identifier.setBorder(new EmptyBorder(0, 5, 0, 5));
167:
168: next.addActionListener(listener);
169: prev.addActionListener(listener);
170: removeButton.addActionListener(listener);
171: createNewButton.addActionListener(listener);
172: moveUpButton.addActionListener(listener);
173: moveDownButton.addActionListener(listener);
174:
175: scrollPane.getViewport().addChangeListener(listener);
176:
177: // list.setDragEnabled(true);
178: list.addMouseListener(listener);
179:
180: list.addListSelectionListener(listener);
181:
182: modelUpdate();
183:
184: createNewButton.setText(i18n.getString("new"));
185: createNewButton.setIcon(i18n.getIcon("icon.new"));
186: removeButton.setIcon(i18n.getIcon("icon.delete"));
187: removeButton.setText(i18n.getString("delete"));
188: nothingSelectedLabel.setHorizontalAlignment(JLabel.CENTER);
189: nothingSelectedLabel
190: .setText(i18n.getString("nothing_selected"));
191:
192: }
193:
194: public JComponent getComponent() {
195: return mainPanel;
196: }
197:
198: public JLabel getStatusBar() {
199: return statusBar;
200: }
201:
202: public JList getList() {
203: return list;
204: }
205:
206: public void setListDimension(Dimension d) {
207: scrollPane.setPreferredSize(d);
208: }
209:
210: public void setMoveButtonVisible(boolean visible) {
211: moveUpButton.setVisible(visible);
212: moveDownButton.setVisible(visible);
213: }
214:
215: public int getSelectedIndex() {
216: return list.getSelectedIndex();
217: }
218:
219: public void select(int index) {
220: list.setSelectedIndex(index);
221: if (index >= 0) {
222: list.ensureIndexIsVisible(index);
223: }
224: }
225:
226: public void setColoredBackgroundEnabled(boolean enable) {
227: coloredBackground = enable;
228: }
229:
230: public boolean isColoredBackgroundEnabled() {
231: return coloredBackground;
232: }
233:
234: private void modelUpdate() {
235: removeButton.setEnabled(list.getMinSelectionIndex() >= 0);
236: moveUpButton.setEnabled(list.getMinSelectionIndex() > 0);
237: moveDownButton.setEnabled(list.getMinSelectionIndex() >= 0
238: && list.getMaxSelectionIndex() < (list.getModel()
239: .getSize() - 1));
240: jointPanel.repaint();
241: }
242:
243: private void editSelectedEntry() {
244: Object selected = list.getSelectedValue();
245: if (selected == null) {
246: cardLayout.first(editPanel);
247: return;
248: } else {
249: cardLayout.last(editPanel);
250: int index = getSelectedIndex();
251: next.setEnabled((index + 1) < list.getModel().getSize());
252: prev.setEnabled(index > 0);
253: Color color = RaplaColorList.getAppointmentColor(0);
254: if (isColoredBackgroundEnabled()) {
255: color = RaplaColorList.getAppointmentColor(index);
256: }
257: identifierPanel.setBackground(color);
258: identifier.setText(String.valueOf(index + 1));
259:
260: callback.actionPerformed(new ActionEvent(this ,
261: ActionEvent.ACTION_PERFORMED, "edit"));
262: }
263: }
264:
265: class Listener extends MouseAdapter implements
266: ListSelectionListener, ActionListener, ChangeListener {
267: public void actionPerformed(ActionEvent evt) {
268: if (evt.getSource() == next) {
269: select(Math.min(list.getModel().getSize() - 1,
270: getSelectedIndex() + 1));
271: } else if (evt.getSource() == prev) {
272: select(Math.max(0, getSelectedIndex() - 1));
273: }
274: if (evt.getSource() == removeButton) {
275: callback.actionPerformed(new ActionEvent(
276: RaplaListEdit.this ,
277: ActionEvent.ACTION_PERFORMED, "remove"));
278: } else if (evt.getSource() == createNewButton) {
279: callback.actionPerformed(new ActionEvent(
280: RaplaListEdit.this ,
281: ActionEvent.ACTION_PERFORMED, "new"));
282: } else if (evt.getSource() == moveUpButton) {
283: callback.actionPerformed(new ActionEvent(
284: RaplaListEdit.this ,
285: ActionEvent.ACTION_PERFORMED, "moveUp"));
286: } else if (evt.getSource() == moveDownButton) {
287: callback.actionPerformed(new ActionEvent(
288: RaplaListEdit.this ,
289: ActionEvent.ACTION_PERFORMED, "moveDown"));
290: }
291: }
292:
293: public void valueChanged(ListSelectionEvent evt) {
294: //if (evt.getValueIsAdjusting())
295: //return;
296: int index = getSelectedIndex();
297: if (index != oldIndex) {
298: oldIndex = index;
299: editSelectedEntry();
300: modelUpdate();
301: }
302: }
303:
304: public void stateChanged(ChangeEvent evt) {
305: if (evt.getSource() == scrollPane.getViewport()) {
306: jointPanel.repaint();
307: }
308: }
309: }
310: }
|