001: package net.sourceforge.squirrel_sql.client.plugin;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import javax.swing.ListSelectionModel;
026: import javax.swing.table.AbstractTableModel;
027: import javax.swing.table.DefaultTableColumnModel;
028: import javax.swing.table.TableColumn;
029: import javax.swing.table.TableColumnModel;
030:
031: import net.sourceforge.squirrel_sql.fw.gui.SortableTable;
032: import net.sourceforge.squirrel_sql.fw.gui.SortableTableModel;
033: import net.sourceforge.squirrel_sql.fw.util.StringManager;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035:
036: public class PluginSummaryTable extends SortableTable {
037: private static final long serialVersionUID = 1L;
038:
039: /** Internationalized strings for this class. */
040: private static final StringManager s_stringMgr = StringManagerFactory
041: .getStringManager(PluginSummaryTable.class);
042:
043: private final static String[] s_hdgs = new String[] {
044: s_stringMgr.getString("PluginSummaryTable.loadAtStartup"),
045: s_stringMgr.getString("PluginSummaryTable.internalName"),
046: s_stringMgr.getString("PluginSummaryTable.name"),
047: s_stringMgr.getString("PluginSummaryTable.loaded"),
048: s_stringMgr.getString("PluginSummaryTable.version"),
049: s_stringMgr.getString("PluginSummaryTable.author"),
050: s_stringMgr.getString("PluginSummaryTable.contributors"), };
051:
052: private final static Class<?>[] s_dataTypes = new Class[] {
053: Boolean.class, String.class, String.class, String.class,
054: String.class, String.class, String.class, };
055:
056: private final static int[] s_columnWidths = new int[] { 100, 100,
057: 150, 50, 50, 100, 100, };
058:
059: public PluginSummaryTable(PluginInfo[] pluginInfo,
060: PluginStatus[] pluginStatus) {
061: super (new MyTableModel(pluginInfo, pluginStatus));
062:
063: setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
064: getTableHeader().setResizingAllowed(true);
065: getTableHeader().setReorderingAllowed(true);
066: setAutoCreateColumnsFromModel(false);
067: setAutoResizeMode(AUTO_RESIZE_LAST_COLUMN);
068:
069: final TableColumnModel tcm = new DefaultTableColumnModel();
070: for (int i = 0; i < s_columnWidths.length; ++i) {
071: final TableColumn col = new TableColumn(i,
072: s_columnWidths[i]);
073: col.setHeaderValue(s_hdgs[i]);
074: tcm.addColumn(col);
075: }
076: setColumnModel(tcm);
077: }
078:
079: PluginStatus[] getPluginStatus() {
080: SortableTableModel stm = getSortableTableModel();
081: SortableTableModel stm2 = (SortableTableModel) stm
082: .getActualModel();
083: MyTableModel tm = (MyTableModel) (stm2.getActualModel());
084: return tm.getPluginStatus();
085: }
086:
087: private static class MyTableModel extends AbstractTableModel {
088: private static final long serialVersionUID = 1L;
089: private ArrayList<PluginData> _pluginData = new ArrayList<PluginData>();
090:
091: MyTableModel(PluginInfo[] pluginInfo,
092: PluginStatus[] pluginStatus) {
093: if (pluginInfo == null) {
094: pluginInfo = new PluginInfo[0];
095: }
096: if (pluginStatus == null) {
097: pluginStatus = new PluginStatus[0];
098: }
099:
100: Map<String, PluginStatus> statuses = new HashMap<String, PluginStatus>();
101: for (int i = 0; i < pluginStatus.length; ++i) {
102: statuses.put(pluginStatus[i].getInternalName(),
103: pluginStatus[i]);
104: }
105:
106: for (int i = 0; i < pluginInfo.length; ++i) {
107: final PluginInfo pi = pluginInfo[i];
108: final PluginStatus ps = statuses.get(pi
109: .getInternalName());
110: final PluginData pd = new PluginData(pi, ps);
111: _pluginData.add(pd);
112: }
113: }
114:
115: synchronized PluginStatus[] getPluginStatus() {
116: final PluginStatus[] ar = new PluginStatus[_pluginData
117: .size()];
118: for (int i = 0; i < ar.length; ++i) {
119: ar[i] = (_pluginData.get(i))._status;
120: }
121: return ar;
122: }
123:
124: public Object getValueAt(int row, int col) {
125: final PluginData pd = _pluginData.get(row);
126: switch (col) {
127: case 0:
128: return Boolean.valueOf(pd._status.isLoadAtStartup());
129: case 1:
130: return pd._info.getInternalName();
131: case 2:
132: return pd._info.getDescriptiveName();
133: case 3:
134: return pd._info.isLoaded() ? s_stringMgr
135: .getString("PluginSummaryTable.true")
136: : s_stringMgr
137: .getString("PluginSummaryTable.false");
138: case 4:
139: return pd._info.getVersion();
140: case 5:
141: return pd._info.getAuthor();
142: case 6:
143: return pd._info.getContributors();
144: default:
145: throw new IndexOutOfBoundsException("" + col);
146: }
147: }
148:
149: public int getRowCount() {
150: return _pluginData.size();
151: }
152:
153: public int getColumnCount() {
154: return s_hdgs.length;
155: }
156:
157: public String getColumnName(int col) {
158: return s_hdgs[col];
159: }
160:
161: public Class<?> getColumnClass(int col) {
162: return s_dataTypes[col];
163: }
164:
165: public boolean isCellEditable(int row, int col) {
166: return col == 0;
167: }
168:
169: public void setValueAt(Object value, int row, int col) {
170: if (col == 0) {
171: final PluginData pd = _pluginData.get(row);
172: boolean loadAtStartup = Boolean.valueOf(
173: value.toString()).booleanValue();
174: pd._status.setLoadAtStartup(loadAtStartup);
175: fireTableCellUpdated(row, col);
176: }
177: if (col == 3) {
178: final PluginData pd = _pluginData.get(row);
179: pd._info.setLoaded(Boolean.valueOf(value.toString()));
180: fireTableCellUpdated(row, col);
181: }
182: }
183:
184: private static class PluginData {
185: final private String _internalName;
186: final private PluginInfo _info;
187: final private PluginStatus _status;
188:
189: PluginData(PluginInfo info, PluginStatus status) {
190: super ();
191: _info = info;
192: _status = (status != null) ? status : new PluginStatus(
193: _info.getInternalName());
194: _internalName = _info.getInternalName();
195: }
196:
197: public String getInternalName() {
198: return _internalName;
199: }
200: }
201: }
202: }
|