001: package isql;
002:
003: import javax.swing.*;
004: import javax.swing.border.*;
005: import javax.accessibility.*;
006: import javax.swing.table.*;
007:
008: import java.awt.Panel;
009: import java.awt.Color;
010: import java.awt.BorderLayout;
011: import java.awt.GridLayout;
012: import java.awt.Font;
013: import java.awt.event.ActionListener;
014: import java.awt.event.ActionEvent;
015: import java.awt.Dimension;
016: import java.awt.Graphics;
017: import java.awt.Color;
018: import java.awt.Rectangle;
019: import java.awt.Container;
020: import java.awt.Component;
021: import java.util.*;
022: import util.*;
023: import java.awt.event.*;
024:
025: /*
026: */
027: public class InternalWindowPanel extends JPanel {
028: // Maker values
029: //JLayeredPane lc;
030: JDesktopPane lc;
031: int makeCount = 0;
032: SQLForm _form = null;
033:
034: public InternalWindowPanel(SQLForm form) {
035: setLayout(new BorderLayout());
036: lc = new JDesktopPane();
037: lc.setOpaque(false);
038: _form = form;
039:
040: add("Center", lc);
041: //JInternalFrame maker = new JInternalFrame("maker",true,true,true,true);
042: // lc.add(maker, JLayeredPane.PALETTE_LAYER);
043:
044: /*
045: maker = createMakerFrame();
046: lc.add(maker, JLayeredPane.PALETTE_LAYER);
047: add("Center", lc);
048: */
049: }
050:
051: /** returns frames whose titles match the given perl pattern
052: */
053: public JInternalFrame[] getMatchingFrames(String pattern) {
054: JInternalFrame[] jifs = lc.getAllFrames();
055: if (".".equals(pattern))
056: return jifs; // optimization
057:
058: List vect = new ArrayList();
059: for (int i = 0; i < jifs.length; i++) {
060: String title = jifs[i].getTitle();
061: if (PerlWrapper.isMatching(pattern, title)) {
062: vect.add(jifs[i]);
063: }
064: }
065: JInternalFrame[] ret = new JInternalFrame[vect.size()];
066: vect.toArray(ret);
067: return ret;
068: }
069:
070: /* this would recieve a JTable inside a scrollpane
071: * and put it up as a internl frame
072: */
073: public void addFrame(TableModel tm, String title) {
074: JTable jt = new JTable(tm);
075: jt
076: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
077: jt.setColumnSelectionAllowed(true);
078: loadActions(jt);
079:
080: // if you modify the next line, horizontal scrolling
081: // wont happen !!! this could be a bug in 1.2/1.3
082: jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
083: TableColumnModel cm = jt.getColumnModel();
084: if (cm == null) {
085: System.err.println("table is null");
086: return;
087: }
088: // TODO check for no rows and return.
089: if (tm instanceof TableMap) {
090: TableMap tmap = (TableMap) tm;
091: int cc = jt.getColumnCount();
092: for (int i = 0; i < cc; i++) {
093: TableColumn tc = cm.getColumn(i);
094: int cw = 4 * tmap.getWidth(i + 1);
095: if (cw < 75)
096: cw = 75;
097: if (cw > 200)
098: cw = 200;
099: tc.setPreferredWidth(cw);
100: tc.setMinWidth(50); // this is required for tables with many columns
101: // but it wont let user make a field less than this
102: // width - he will not be able to see columns on right
103: }
104: }
105:
106: JScrollPane panel = new JScrollPane(jt);
107: addFrame(panel, title);
108: }
109:
110: public void addFrame(JComponent jc, String title) {
111: JInternalFrame w;
112: w = new JInternalFrame(title, true, true, true, true);
113: /*
114: w.setClosable(closeBox.isSelected());
115: w.setMaximizable(maxBox.isSelected());
116: w.setIconifiable(iconBox.isSelected());
117: */
118: //w.setResizable(resizeBox.isSelected());
119: Integer layer = JLayeredPane.PALETTE_LAYER;
120: int count = lc.getAllFrames().length;
121: count++;
122: makeCount++;
123: Dimension size = lc.getSize();
124: JTable jt = getJTable((JScrollPane) jc);
125: //Dimension preferredsize = jt.getPreferredScrollableViewportSize();
126: //too large like 343 pix
127: int htpixels = (jt.getRowCount() + 5)
128: * ((JTable) jt).getRowHeight();
129: int prefht2 = Math.min(htpixels, size.height);
130: switch (count) {
131: case 1:
132: w.setBounds(0, 0, size.width, prefht2);
133: break;
134: //case 1: w.setBounds(0,0, size.width, size.height/2); break;
135: ///case 1: w.setBounds(20,10, size.width/4, size.height/4); break;
136: case 2:
137: w.setBounds(0, size.height / 2, size.width, prefht2);
138: //case 2: w.setBounds(20,size.height/2,size.width, size.height/2);
139: //case 2: w.setBounds(0,size.height/2,size.width/4, size.height/4);
140: break;
141: default:
142: w.setBounds(0, 20 * (makeCount % 10), size.width, prefht2);
143: //w.setBounds(20+(makeCount*5), 40*(makeCount%5), size.width/4, size.height/4);
144: }
145: w.setContentPane(jc);
146:
147: lc.add(w, layer);
148: w.setVisible(true);
149: try {
150: w.setSelected(true);
151: } catch (java.beans.PropertyVetoException e2) {
152: System.err.println("JIF EXC" + e2.toString());
153: }
154: //drawStuff();
155: }
156:
157: private void makepopup(JTable jt, JMenuItem jmia[]) {
158: final JTable jtf = jt;
159: final JMenuItem jmiaf[] = jmia;
160:
161: jt.addMouseListener(new MouseAdapter() {
162: public void mousePressed(MouseEvent e) {
163: if (e.isPopupTrigger()) {
164: JPopupMenu jpm = new JPopupMenu();
165: for (int i = 0; i < jmiaf.length; i++) {
166: jpm.add(jmiaf[i]);
167: }
168: jpm.pack();
169: jpm.show(jtf, e.getX(), e.getY());
170: jpm.setVisible(true);
171: jpm.requestFocus();
172: }
173: }
174: });
175: }
176:
177: public void drawStuff() { // XXXX
178:
179: java.awt.Graphics g = lc.getGraphics();
180: JInternalFrame[] jifs = lc.getAllFrames();
181: int prevx = 0, prevy = 0;
182: int x, y;
183: java.awt.Rectangle r1 = lc.getBounds(new Rectangle());
184: g.clearRect(r1.x, r1.y, r1.width, r1.height);
185: lc.paintImmediately(r1.x, r1.y, r1.width, r1.height);
186: for (int i = 0; i < jifs.length; i++) {
187: java.awt.Rectangle r = jifs[i].getBounds(new Rectangle());
188: y = r.y + 40;
189: //x = (r.getX()+r.getWidth())/2;
190: x = r.x;
191: // draw linw
192: g.drawLine(x, y, prevx, prevy);
193: prevx = x;
194: prevy = y;
195: }
196: } // draw stuff
197:
198: /** Load actions from table actions class.
199: * RK added on 20031225 20:43:25
200: * Load all actions automatically, now that TableA gives a map.
201: */
202: public void loadActions(JTable jt) {
203:
204: Map actions = TableActions.getActionMap(_form, jt);
205: JMenuItem jmia[] = new JMenuItem[actions.size()];
206: Iterator it = actions.keySet().iterator();
207: int i = 0;
208: while (it.hasNext()) {
209: String s = (String) it.next();
210: AbstractAction a = (AbstractAction) actions.get(s);
211: jt.getActionMap().put(s, a);
212: jmia[i] = new JMenuItem(a);
213: // RK added on 20040103 22:55:21 added accelerators
214: String acc = (String) a.getValue("accelerator");
215: if (acc != null)
216: jmia[i].setAccelerator(KeyStroke.getKeyStroke(acc));
217: i++;
218:
219: }
220: makepopup(jt, jmia);
221: // these need to be picked from the file TODO
222: jt.getInputMap().put(KeyStroke.getKeyStroke("control DELETE"),
223: TableActions.deleteAction);
224: jt.getInputMap().put(
225: KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
226: TableActions.deleteAction);
227: jt.getInputMap().put(KeyStroke.getKeyStroke("alt U"),
228: TableActions.updateAction);
229: jt.getInputMap().put(
230: KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),
231: TableActions.insertAction);
232: jt.getInputMap().put(KeyStroke.getKeyStroke('/'),
233: TableActions.searchAction);
234: jt.getInputMap().put(KeyStroke.getKeyStroke('o'),
235: TableActions.sortAction);
236: jt.getInputMap().put(KeyStroke.getKeyStroke('O'),
237: TableActions.reversesortAction);
238: jt.getInputMap().put(KeyStroke.getKeyStroke("control O"),
239: TableActions.asksortAction);
240:
241: //Map bindings = _form.TNS.getBindingsForFrame();
242: //IsqlUtil.bindToTable(bindings, jt);
243: Map bindings = _form.getBindings().getBindingsForFrame();
244: _form.getBindings().bindToTable(bindings, jt);
245: //System.out.println( "Loaded.");
246:
247: }
248:
249: /** add the given binding to all jtables, for the given perl
250: * pattern. Use a dot (".") for all tables.
251: */
252: public void addBinding(String pattern, String key, String action) {
253: JInternalFrame[] jifs = this .getMatchingFrames(pattern);
254: for (int i = 0; i < jifs.length; i++) {
255: System.out.print("Binding " + key + " to Frame:"
256: + jifs[i].getTitle());
257: JScrollPane jsp = (JScrollPane) jifs[i].getContentPane();
258: JViewport jvp = jsp.getViewport();
259: Component co = jvp.getView();
260: if (!(co instanceof JTable))
261: continue;
262: JTable jt = (JTable) co;
263: key = key.replace('-', ' ');
264: KeyStroke ks = KeyStroke.getKeyStroke(key);
265: if (ks == null) {
266: System.err.println("Binding failed for key:" + key);
267: return;
268: }
269: jt.getInputMap().put(ks, action);
270: System.out.println(" Done.");
271: } // for
272: } // addBinding
273:
274: /** return the jtable from a jsp !!!.
275: */
276: public JTable getJTable(JScrollPane jsp) {
277: //JScrollPane jsp = (JScrollPane) jif.getContentPane();
278: JViewport jvp = jsp.getViewport();
279: Component co = jvp.getView();
280: if (!(co instanceof JTable))
281: return null;
282: ;
283: return (JTable) co;
284: }
285:
286: /** return the jtable from a jif !!!.
287: */
288: public JTable getJTable(JInternalFrame jif) {
289: JScrollPane jsp = (JScrollPane) jif.getContentPane();
290: JViewport jvp = jsp.getViewport();
291: Component co = jvp.getView();
292: if (!(co instanceof JTable))
293: return null;
294: return (JTable) co;
295: }
296: } // class
|