001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.TableFrameXmlBean;
004:
005: import javax.swing.*;
006: import javax.swing.border.LineBorder;
007: import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
008: import javax.swing.plaf.basic.BasicInternalFrameUI;
009: import javax.imageio.ImageReader;
010: import javax.imageio.stream.ImageInputStream;
011: import javax.imageio.stream.ImageInputStreamImpl;
012: import java.awt.*;
013: import java.awt.geom.AffineTransform;
014: import java.net.URL;
015:
016: public class TableFrame extends JInternalFrame {
017: GraphTextAreaFactory txtColumsFactory;
018: JScrollPane scrollPane;
019: private MyUI _myUI;
020: private Zoomer _zoomer;
021: private ZoomerListener _zoomerListener;
022:
023: public TableFrame(String tableName, TableFrameXmlBean xmlBean,
024: TableToolTipProvider toolTipProvider, Zoomer zoomer) {
025: _zoomer = zoomer;
026:
027: scrollPane = new JScrollPane();
028: scrollPane.setBorder(null);
029:
030: getContentPane().add(scrollPane);
031:
032: setMaximizable(false);
033: setClosable(true);
034: setIconifiable(false);
035:
036: setTitle(tableName);
037: setBackground(new Color(255, 255, 204));
038:
039: setResizable(true);
040:
041: setFrameIcon(null);
042:
043: _myUI = new MyUI(this );
044: setUI(_myUI);
045:
046: txtColumsFactory = new GraphTextAreaFactory(toolTipProvider,
047: zoomer);
048: scrollPane.setViewportView(txtColumsFactory.getComponent(zoomer
049: .isEnabled()));
050:
051: if (null != xmlBean) {
052: double zoom = _zoomer.getZoom();
053:
054: Rectangle r = new Rectangle();
055: r.x = (int) (zoom * xmlBean.getX() + 0.5);
056: r.y = (int) (zoom * xmlBean.getY() + 0.5);
057: r.width = (int) (zoom * xmlBean.getWidht() + 0.5);
058: r.height = (int) (zoom * xmlBean.getHeight() + 0.5);
059: setBounds(r);
060: setClosable(!_zoomer.isEnabled());
061: }
062:
063: _zoomerListener = new ZoomerListener() {
064: public void zoomChanged(double newZoom, double oldZoom,
065: boolean adjusting) {
066: }
067:
068: public void zoomEnabled(boolean b) {
069: onZoomEnabled(b);
070: }
071:
072: public void setHideScrollBars(boolean b) {
073: }
074: };
075:
076: setBorder(new LineBorder(Color.BLACK));
077: }
078:
079: public void setVisible(boolean b) {
080: if (null != _zoomer) {
081: if (b) {
082: _zoomer.addZoomListener(_zoomerListener);
083: onZoomEnabled(_zoomer.isEnabled());
084: } else {
085: _zoomer.removeZoomListener(_zoomerListener);
086: }
087: }
088:
089: super .setVisible(b);
090: }
091:
092: private void onZoomEnabled(boolean b) {
093: scrollPane.setViewportView(txtColumsFactory
094: .getComponent(_zoomer.isEnabled()));
095: setClosable(!b);
096: }
097:
098: public TableFrame.MyTitlePaneUI getTitlePane() {
099: return _myUI.getTitlePane();
100: }
101:
102: public TableFrameXmlBean getXmlBean() {
103: TableFrameXmlBean ret = new TableFrameXmlBean();
104:
105: double zoom = _zoomer.getZoom();
106:
107: Rectangle bounds = getBounds();
108: ret.setX((int) (bounds.x / zoom + 0.5));
109: ret.setY((int) (bounds.y / zoom + 0.5));
110: ret.setWidht((int) (bounds.width / zoom + 0.5));
111: ret.setHeight((int) (bounds.height / zoom + 0.5));
112:
113: return ret;
114:
115: }
116:
117: class MyUI extends BasicInternalFrameUI {
118: public MyUI(JInternalFrame frame) {
119: super (frame);
120: }
121:
122: protected JComponent createNorthPane(JInternalFrame w) {
123: titlePane = new MyTitlePaneUI(w);
124: return titlePane;
125: }
126:
127: public TableFrame.MyTitlePaneUI getTitlePane() {
128: return (MyTitlePaneUI) _myUI.titlePane;
129: }
130:
131: }
132:
133: class MyTitlePaneUI extends BasicInternalFrameTitlePane {
134: public static final int UNZOOMED_PREF_HEIGHT = 18;
135:
136: public MyTitlePaneUI(JInternalFrame f) {
137: super (f);
138: }
139:
140: protected void installDefaults() {
141: super .installDefaults();
142: URL resource = TableFrame.class
143: .getResource("/net/sourceforge/squirrel_sql/plugins/graph/images/win_bigclose-rollover.gif");
144: closeIcon = new ImageIcon(resource);
145: selectedTextColor = Color.black;
146: notSelectedTextColor = Color.black;
147: setFont(new Font(getFont().getFontName(), Font.BOLD,
148: getFont().getSize()));
149: }
150:
151: protected void paintTitleBackground(Graphics g) {
152: g.setColor(new Color(255, 255, 220));
153: g.fillRect(0, 0, getWidth(), getHeight());
154: }
155:
156: public void paintComponent(Graphics g) {
157: paintTitleBackground(g);
158:
159: if (frame.getTitle() != null) {
160: boolean isSelected = frame.isSelected();
161: Font f = g.getFont();
162: g.setFont(getFont());
163: if (isSelected)
164: g.setColor(selectedTextColor);
165: else
166: g.setColor(notSelectedTextColor);
167:
168: // Center text vertically.
169: FontMetrics fm = g.getFontMetrics();
170:
171: double s = _zoomer.getZoom();
172: int baseline = ((int) (getHeight() / s)
173: + fm.getAscent() - fm.getLeading() - fm
174: .getDescent()) / 2;
175:
176: int titleX;
177: Rectangle r = new Rectangle(0, 0, 0, 0);
178: if (frame.isIconifiable())
179: r = iconButton.getBounds();
180: else if (frame.isMaximizable())
181: r = maxButton.getBounds();
182: else if (frame.isClosable())
183: r = closeButton.getBounds();
184: int titleW;
185:
186: String title = frame.getTitle();
187:
188: if (r.x == 0)
189: r.x = frame.getWidth() - frame.getInsets().right;
190: titleX = menuBar.getX() + menuBar.getWidth() + 2;
191: titleW = (int) ((r.x - titleX - 3) / _zoomer.getZoom() + 0.5);
192: title = getTitle(frame.getTitle(), fm, titleW);
193:
194: Graphics2D g2d = (Graphics2D) g;
195: AffineTransform origTrans = g2d.getTransform();
196:
197: AffineTransform at = new AffineTransform(origTrans);
198: at.scale(_zoomer.getZoom(), _zoomer.getZoom());
199: g2d.setTransform(at);
200:
201: g.drawString(title, titleX, baseline);
202:
203: g2d.setTransform(origTrans);
204:
205: g.setFont(f);
206: }
207: }
208:
209: protected LayoutManager createLayout() {
210: return new MyTitlePaneLayout();
211: }
212:
213: public Dimension getPreferredSize() {
214: Dimension ret = super .getPreferredSize();
215:
216: ret.height = (int) (UNZOOMED_PREF_HEIGHT
217: * _zoomer.getZoom() + 0.5);
218: return ret;
219: }
220:
221: /**
222: * This removes the system menu
223: * @return
224: */
225: protected JMenuBar createSystemMenuBar() {
226: menuBar = new JMenuBar() {
227: public void setSize(int width, int height) {
228: super .setSize(0, 0);
229: }
230:
231: public void setBounds(int x, int y, int width,
232: int height) {
233: super .setBounds(0, 0, 0, 0);
234: }
235: };
236: menuBar.setBorderPainted(false);
237: menuBar.setSize(0, 0);
238: menuBar.setBounds(0, 0, 0, 0);
239: return menuBar;
240: }
241:
242: class MyTitlePaneLayout extends
243: BasicInternalFrameTitlePane.TitlePaneLayout {
244:
245: public Dimension minimumLayoutSize(Container c) {
246: Dimension ret = super.minimumLayoutSize(c);
247: ret.width *= _zoomer.getZoom();
248: ret.height *= _zoomer.getZoom();
249: return ret;
250: }
251: }
252:
253: }
254: }
|