001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.awt.BorderLayout;
018: import java.awt.Color;
019: import java.awt.Container;
020: import java.awt.Dimension;
021: import java.awt.Graphics;
022: import java.awt.Graphics2D;
023: import java.awt.Rectangle;
024: import java.awt.Shape;
025: import java.awt.SystemColor;
026: import java.util.ArrayList;
027:
028: import javax.swing.JPanel;
029: import javax.swing.text.StyleConstants;
030:
031: /* Stasus Bar class */
032:
033: public class StatusBar extends JPanel //implements Runnable
034: {
035: public static final int MIN_HEIGHT = 20;
036:
037: protected ArrayList mPanels = new ArrayList();
038: protected boolean mShowBorders = true;
039:
040: //protected JProgressBar mProgress = new JProgressBar();
041:
042: public StatusBar() {
043: super ();
044: /*mProgress.setMinimum(0);
045: mProgress.setMaximum(100);
046: mProgress.setMinimumSize(new Dimension(100, 20));
047: mProgress.setSize(new Dimension(100, 20));*/
048:
049: setLayout(new BorderLayout() {
050: public Dimension minimumLayoutSize(Container target) {
051: Dimension lDim = super .minimumLayoutSize(target);
052: if (lDim.height < getMinHeight())
053: lDim.height = getMinHeight();
054: return lDim;
055: }
056:
057: public Dimension preferredLayoutSize(Container target) {
058: Dimension lDim = super .preferredLayoutSize(target);
059: if (lDim.height < getMinHeight())
060: lDim.height = getMinHeight();
061: return lDim;
062: }
063:
064: public void layoutContainer(Container target) {
065: //???
066: }
067: });
068: }
069:
070: public void addPanel(String pCaption, int pWidth) {
071: mPanels.add(new StatusBarPanel(pCaption, pWidth));
072: }
073:
074: public void addPanel(String pCaption, int pWidth, int pAlign) {
075: mPanels.add(new StatusBarPanel(pCaption, pWidth, pAlign));
076: }
077:
078: public void setPanelText(int pIndex, String pText) {
079: StatusBarPanel lPanel = getPanel(pIndex);
080: lPanel.setText(pText);
081: repaint();
082: }
083:
084: public void paint(Graphics g) {
085: super .paint(g);
086: calcPanelsBounds();
087: for (int i = 0; i < getPanelsCount(); i++)
088: paintStatusPanel(g, getPanel(i));
089: }
090:
091: /* Properties Accessors */
092:
093: // return panels count
094: public int getPanelsCount() {
095: return mPanels.size();
096: }
097:
098: // return panel
099: public StatusBarPanel getPanel(int pIndex) {
100: return (StatusBarPanel) mPanels.get(pIndex);
101: }
102:
103: public boolean isShowBorders() {
104: return mShowBorders;
105: }
106:
107: public void setShowBorders(boolean pShow) {
108: if (mShowBorders != pShow) {
109: mShowBorders = pShow;
110: repaint();
111: }
112: }
113:
114: /* Auxilary methods */
115:
116: // get minimal control height
117: protected int getMinHeight() {
118: return MIN_HEIGHT;
119: }
120:
121: // calculate Panels bounds
122: protected void calcPanelsBounds() {
123: Dimension lSize = getSize();
124: Rectangle lRect = new Rectangle(0, 0, 0, 0);
125:
126: lRect.height = getMinHeight();
127: for (int i = 0; i < mPanels.size(); i++) {
128: StatusBarPanel lPanel = getPanel(i);
129: if (lPanel != null) {
130: lRect.x += lRect.width;
131: lRect.width = lPanel.mWidth;
132: lPanel.mBounds = new Rectangle(lRect);
133: if (i < mPanels.size() - 1)
134: lRect.x += 2;
135: }
136: }
137:
138: if (lRect.x + lRect.width < lSize.width) {
139: lRect.width = lSize.width - lRect.x;
140: StatusBarPanel lPanel = getPanel(mPanels.size() - 1);
141: lPanel.mBounds = new Rectangle(lRect);
142: }
143: }
144:
145: // paint ststus panel
146: protected void paintStatusPanel(Graphics g, StatusBarPanel pPanel) {
147: if (mShowBorders)
148: paintBorder(g, pPanel.mBounds);
149: paintString(g, pPanel.mBounds, pPanel.mText, pPanel.mAlign);
150: }
151:
152: // draw border
153: protected void paintBorder(Graphics g, Rectangle pRect) {
154: Color lColor = getBackground();
155:
156: g.setColor(lColor.darker());
157: g.drawLine(pRect.x, pRect.y + 1, pRect.x + pRect.width - 1,
158: pRect.y + 1);
159: g.drawLine(pRect.x, pRect.y + 1, pRect.x, pRect.y
160: + pRect.height - 1);
161:
162: g.setColor(lColor.brighter());
163: g.drawLine(pRect.x, pRect.y + pRect.height - 1, pRect.x
164: + pRect.width - 1, pRect.y + pRect.height - 1);
165: g.drawLine(pRect.x + pRect.width - 1, pRect.y + 1, pRect.x
166: + pRect.width - 1, pRect.y + pRect.height - 1);
167: }
168:
169: // draw string
170: protected void paintString(Graphics g, Rectangle pRect,
171: String pText, int pAlign) {
172: if (pText == null || pText.length() == 0)
173: return;
174:
175: Graphics2D g2 = (Graphics2D) g;
176:
177: int ld = (int) getFontMetrics(g.getFont()).getStringBounds(
178: pText, g).getWidth();
179: int lyStep = getFontMetrics(g.getFont()).getHeight() / 2;
180: lyStep = (pRect.height + lyStep) / 2 + 2;
181:
182: int x = pRect.x;
183: int y = pRect.y + lyStep;
184:
185: switch (pAlign) {
186: case StyleConstants.ALIGN_LEFT:
187: x += 2;
188: break;
189: case StyleConstants.ALIGN_RIGHT:
190: x += (pRect.width - ld - 2);
191: break;
192: case StyleConstants.ALIGN_CENTER:
193: x += ((pRect.width - ld) / 2);
194: break;
195: }
196:
197: Shape lOldClip = g2.getClip();
198: g2.setClip(pRect);
199: g2.setColor(SystemColor.controlText);
200: g2.drawString(pText, x, y);
201: g2.setClip(lOldClip);
202: }
203:
204: /* StstusBar panels class */
205:
206: public class StatusBarPanel {
207: private Rectangle mBounds = new Rectangle(0, 0, 0, 0);
208: private String mText = "";
209: private int mWidth = 0;
210: private int mAlign = StyleConstants.ALIGN_LEFT;
211:
212: public StatusBarPanel(String pText, int pWidth) {
213: mText = pText;
214: mWidth = pWidth;
215: }
216:
217: public StatusBarPanel(String pText, int pWidth, int pAlign) {
218: mText = pText;
219: mWidth = pWidth;
220: mAlign = pAlign;
221: }
222:
223: public String getText() {
224: return mText;
225: }
226:
227: public void setText(String pText) {
228: mText = pText;
229: }
230:
231: public int getWidht() {
232: return mWidth;
233: }
234:
235: public void setWidht(int pWidth) {
236: mWidth = pWidth;
237: }
238:
239: public int getAlign() {
240: return mAlign;
241: }
242:
243: public void setAlign(int pAlign) {
244: mAlign = pAlign;
245: }
246: }
247:
248: /* public void showProgress(int percent)
249: {
250: // mProgress.setValue(percent);
251: }
252:
253: public void incProgress(int delataPercent)
254: {
255: // mProgress.setValue(mProgress.getValue() + delataPercent);
256: }
257:
258: public synchronized void doFakeProgress(String s, int work)
259: {
260: mStatusText = s;
261: showStatus(mStatusText + "... not implemented yet ...");
262: // mProgress.setMaximum(work);
263: // mProgress.setValue(0);
264: Thread t = new Thread(this);
265: t.start();
266: }
267:
268: public synchronized void run()
269: {
270: int work = mProgress.getMaximum();
271: for (int i = 0; i < work; i++)
272: {
273: mProgress.setValue(i);
274: repaint();
275: doWait(10);
276: }
277: showStatus(mStatusText + "... done.");
278: repaint();
279: doWait(1000);
280: mProgress.setValue(0);
281: showStatus("");
282: repaint();
283: }
284:
285: // wait
286: private void doWait(int ATimeOut)
287: {
288: try
289: {
290: wait(ATimeOut);
291: }
292: catch (Exception ex)
293: {
294: //????
295: }
296: } */
297: }
|