001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.application.studio.history.viewer.ui;
034:
035: import org.libresource.so6.application.studio.history.viewer.util.WorkspaceColorManager;
036:
037: import java.awt.BorderLayout;
038: import java.awt.Color;
039: import java.awt.Component;
040: import java.awt.event.MouseEvent;
041: import java.awt.event.MouseListener;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Hashtable;
046: import java.util.Iterator;
047:
048: import javax.swing.AbstractListModel;
049: import javax.swing.JColorChooser;
050: import javax.swing.JLabel;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.JScrollPane;
054: import javax.swing.ListCellRenderer;
055:
056: /**
057: * @author smack
058: */
059: public class ColorManagerImpl extends JPanel implements
060: WorkspaceColorManager, MouseListener {
061: private Hashtable colorTable;
062: private ArrayList wsNameList;
063: private JList list = null;
064:
065: public ColorManagerImpl(Collection wsNameList) {
066: this .colorTable = new Hashtable();
067: this .wsNameList = new ArrayList();
068:
069: for (Iterator i = wsNameList.iterator(); i.hasNext();) {
070: String wsName = (String) i.next();
071: this .wsNameList.add(wsName);
072: this .colorTable.put(wsName, Color.white);
073: }
074:
075: // init ui
076: list = new JList(new ListAdapter());
077: list.addMouseListener(this );
078: list.setCellRenderer(new ListRenderer());
079:
080: //
081: this .setLayout(new BorderLayout());
082: this .add(new JScrollPane(list), BorderLayout.CENTER);
083: }
084:
085: /**
086: * interface
087: */
088: public Color getColorOfWorkspace(String wsName) {
089: return (Color) colorTable.get(wsName);
090: }
091:
092: // Mouse listener
093: public void mouseClicked(MouseEvent e) {
094: if ((e.getClickCount() == 2) && (list.getSelectedIndex() != -1)) {
095: Color result = JColorChooser.showDialog(this , "User color",
096: Color.WHITE);
097:
098: if (result != null) {
099: colorTable.put(list.getSelectedValue(), result);
100: }
101: }
102: }
103:
104: public void mouseEntered(MouseEvent e) {
105: }
106:
107: public void mouseExited(MouseEvent e) {
108: }
109:
110: public void mousePressed(MouseEvent e) {
111: }
112:
113: public void mouseReleased(MouseEvent e) {
114: }
115:
116: /**
117: * ui
118: */
119:
120: // renderer
121: private class ListRenderer extends JLabel implements
122: ListCellRenderer {
123: public ListRenderer() {
124: setOpaque(true);
125: }
126:
127: public Component getListCellRendererComponent(JList list,
128: Object value, int index, boolean isSelected,
129: boolean cellHasFocus) {
130: this .setText((String) value);
131: this .setBackground((Color) colorTable.get(value));
132:
133: return this ;
134: }
135: }
136:
137: // List adapter
138: private class ListAdapter extends AbstractListModel {
139: public void update() {
140: fireContentsChanged(this , 0, getSize());
141: }
142:
143: public Object getElementAt(int index) {
144: return wsNameList.get(index);
145: }
146:
147: public int getSize() {
148: return wsNameList.size();
149: }
150: }
151: }
|