001: package de.ixdb.squirrel_sql.plugins.cache;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.client.session.mainpanel.IMainPanelTab;
005: import net.sourceforge.squirrel_sql.fw.gui.ButtonTableHeader;
006: import net.sourceforge.squirrel_sql.fw.gui.SortableTableModel;
007: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetViewerTablePanel;
008: import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
009: import net.sourceforge.squirrel_sql.fw.datasetviewer.CellDataPopup;
010:
011: import javax.swing.*;
012: import javax.swing.table.*;
013: import java.awt.*;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.awt.event.MouseAdapter;
017: import java.awt.event.MouseEvent;
018:
019: public class ProcessListTab implements IMainPanelTab {
020: ProcessListPanel _pnlProcessList;
021: private ISession _session;
022: private ProcessData[] _procData;
023: private ProcessListTabListener _processListTabListener;
024:
025: public ProcessListTab(ProcessData[] procData,
026: ProcessListTabListener processListTabListener) {
027: _procData = procData;
028:
029: _processListTabListener = processListTabListener;
030: _pnlProcessList = new ProcessListPanel();
031:
032: DefaultTableModel dtm = new DefaultTableModel() {
033: public boolean isCellEditable(int row, int column) {
034: return false;
035: }
036:
037: public Object getValueAt(int row, int column) {
038: return onGetValueAt(row, column);
039: }
040:
041: public String getColumnName(int column) {
042: return ProcessData.getColumns()[column];
043: }
044:
045: public int getRowCount() {
046: if (null == ProcessListTab.this ) {
047: // I have seen the reference to the outer class being null
048: // when this method is called.
049: // I have seen it only with the runtime jars
050: // and on Linux.
051: // I could not reproduce in my IDE.
052: return 0;
053: } else {
054: return ProcessListTab.this ._procData.length;
055: }
056: }
057:
058: public int getColumnCount() {
059: return ProcessData.getColumns().length;
060: }
061:
062: };
063:
064: SortableTableModel stm = (SortableTableModel) _pnlProcessList.tblProcessList
065: .getModel();
066: stm.setActualModel(dtm);
067:
068: DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer() {
069: public Component getTableCellRendererComponent(
070: JTable table, Object value, boolean isSelected,
071: boolean hasFocus, int row, int column) {
072: Component rend = super
073: .getTableCellRendererComponent(table, value,
074: isSelected, hasFocus, row, column);
075: if (null != value && 0 < value.toString().indexOf('\n')) {
076: rend.setBackground(Color.cyan);
077: } else {
078: if (isSelected) {
079: setBackground(table.getSelectionBackground());
080: } else {
081: setBackground(table.getBackground());
082: }
083: }
084: return rend;
085: }
086: };
087:
088: final TableColumnModel tcm = new DefaultTableColumnModel();
089: for (int i = 0; i < ProcessData.getColumns().length; ++i) {
090: final TableColumn col = new TableColumn(i);
091: col.setHeaderValue(ProcessData.getColumns()[i]);
092: col.setCellRenderer(dtcr);
093: tcm.addColumn(col);
094: }
095:
096: _pnlProcessList.tblProcessList.setColumnModel(tcm);
097:
098: _pnlProcessList.btnRefresh
099: .addActionListener(new ActionListener() {
100: public void actionPerformed(ActionEvent e) {
101: onRefresh();
102: }
103: });
104:
105: _pnlProcessList.btnTerminate
106: .addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent e) {
108: onTerminate();
109: }
110: });
111:
112: _pnlProcessList.btnClose
113: .addActionListener(new ActionListener() {
114: public void actionPerformed(ActionEvent e) {
115: onClose();
116: }
117: });
118:
119: _pnlProcessList.tblProcessList
120: .addMouseListener(new MouseAdapter() {
121: public void mousePressed(MouseEvent evt) {
122: if (evt.getClickCount() == 2) {
123: // figure out which column the user clicked on
124: // so we can pass in the right column description
125:
126: Point pt = evt.getPoint();
127: int col = _pnlProcessList.tblProcessList
128: .columnAtPoint(pt);
129: ColumnDisplayDefinition dumDef = new ColumnDisplayDefinition(
130: 50, "Detail");
131: CellDataPopup.showDialog(
132: _pnlProcessList.tblProcessList,
133: dumDef, evt, false);
134: }
135: }
136: });
137: }
138:
139: private void onClose() {
140: _processListTabListener.closeRequested(_session);
141: }
142:
143: private void onTerminate() {
144: int selectedRow = _pnlProcessList.tblProcessList
145: .getSelectedRow();
146: selectedRow = _pnlProcessList.tblProcessList
147: .getSortableTableModel().transfromToModelRow(
148: selectedRow);
149:
150: String msg;
151: if (-1 == selectedRow) {
152: msg = "No process selected.\nPlease select the Process you wish to terminate.";
153: JOptionPane.showMessageDialog(_session.getApplication()
154: .getMainFrame(), msg);
155: return;
156: }
157:
158: msg = "Do you really wish to terminate the selected process?";
159: if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(
160: _session.getApplication().getMainFrame(), msg,
161: "Terminate process", JOptionPane.YES_NO_CANCEL_OPTION)) {
162: return;
163: }
164:
165: int termRet = _processListTabListener.terminateRequested(
166: _session, _procData[selectedRow]);
167:
168: if (1 != termRet) {
169: msg = "Terminate should return 1 but returned " + termRet
170: + ".\nMaybe terminate didn't succeed.";
171: JOptionPane.showMessageDialog(_session.getApplication()
172: .getMainFrame(), msg);
173: return;
174: }
175: }
176:
177: private void onRefresh() {
178: int selectedRow = _pnlProcessList.tblProcessList
179: .getSelectedRow();
180:
181: ProcessData old = null;
182: if (-1 != selectedRow) {
183: old = _procData[selectedRow];
184: }
185:
186: _procData = _processListTabListener.refreshRequested(_session);
187:
188: SortableTableModel stm = (SortableTableModel) _pnlProcessList.tblProcessList
189: .getModel();
190: ((DefaultTableModel) stm.getActualModel())
191: .fireTableDataChanged();
192: stm.fireTableDataChanged();
193:
194: ButtonTableHeader bth = (ButtonTableHeader) _pnlProcessList.tblProcessList
195: .getTableHeader();
196:
197: if (-1 != bth.getCurrentlySortedColumnIdx()) {
198: stm.sortByColumn(bth.getCurrentlySortedColumnIdx(), bth
199: .isAscending());
200: }
201:
202: _pnlProcessList.tblProcessList.repaint();
203:
204: if (null != old) {
205: for (int i = 0; i < _procData.length; i++) {
206: if (old.job == _procData[i].job) {
207: _pnlProcessList.tblProcessList.getSelectionModel()
208: .setSelectionInterval(i, i);
209: break;
210: }
211: }
212: }
213:
214: }
215:
216: private Object onGetValueAt(int row, int column) {
217: try {
218: return ProcessData.class.getFields()[column]
219: .get(_procData[row]);
220: } catch (IllegalAccessException e) {
221: throw new RuntimeException(e);
222: }
223: }
224:
225: public String getTitle() {
226: return "Processes";
227: }
228:
229: public String getHint() {
230: return getTitle();
231: }
232:
233: public Component getComponent() {
234: return _pnlProcessList;
235: }
236:
237: public void setSession(ISession session) {
238: _session = session;
239: }
240:
241: public void sessionClosing(ISession session) {
242: _session = null;
243: }
244:
245: public void select() {
246: }
247: }
|