001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.log.monitoring;
034:
035: import java.util.ArrayList;
036: import java.util.Observable;
037:
038: import javax.swing.JProgressBar;
039:
040: /**
041: * @author smack
042: */
043: public class TreeContext extends Observable {
044: private double globalState = 0;
045: private double localState = 0;
046: private double lastGlobalState = 0;
047:
048: //
049: private JProgressBar globalProgress;
050: private JProgressBar localProgress;
051:
052: //
053: private ArrayList treePartPathMax;
054: private ArrayList treePartPathCurrent;
055:
056: public TreeContext() {
057: treePartPathMax = new ArrayList();
058: treePartPathCurrent = new ArrayList();
059: }
060:
061: public void startPart(int nbPart) {
062: treePartPathMax.add(new Integer(nbPart));
063: treePartPathCurrent.add(new Integer(0));
064: setLocalState(-1);
065: }
066:
067: public void startPart() {
068: startPart(1);
069: }
070:
071: public void endPart() {
072: treePartPathMax.remove(treePartPathMax.size() - 1);
073: treePartPathCurrent.remove(treePartPathCurrent.size() - 1);
074:
075: if (treePartPathCurrent.size() > 0) {
076: Integer value = ((Integer) treePartPathCurrent
077: .remove(treePartPathCurrent.size() - 1));
078: value = new Integer(value.intValue() + 1);
079: treePartPathCurrent.add(value);
080:
081: //
082: setGlobalState(computeGlobalState());
083: lastGlobalState = globalState;
084: } else {
085: setGlobalState(100);
086: setLocalState(100);
087: }
088: }
089:
090: public void setLocalState(double from, double to, double current) {
091: double pourcent = (current - from + 1) / (to - from + 1);
092: double ref = 100;
093:
094: // ref
095: int nbElements = treePartPathMax.size();
096:
097: for (int i = 0; i < nbElements; i++) {
098: int valueMax = ((Integer) treePartPathMax.get(i))
099: .intValue();
100: int valueCurrent = ((Integer) treePartPathCurrent.get(i))
101: .intValue();
102: ref /= valueMax;
103: }
104:
105: //System.out.println("ref= " + ref);
106: //
107: setGlobalState(lastGlobalState + (ref * pourcent));
108: setLocalState(100 * pourcent);
109: }
110:
111: private double computeGlobalState() {
112: double result = 0;
113: double ref = 100;
114: int nbElements = treePartPathMax.size();
115:
116: for (int i = 0; i < nbElements; i++) {
117: int valueMax = ((Integer) treePartPathMax.get(i))
118: .intValue();
119: int valueCurrent = ((Integer) treePartPathCurrent.get(i))
120: .intValue();
121: ref /= valueMax;
122: result += (ref * valueCurrent);
123: }
124:
125: return result;
126: }
127:
128: //
129: public double getGlobalState() {
130: return globalState;
131: }
132:
133: public double getLocalState() {
134: return localState;
135: }
136:
137: public String toString() {
138: return "Global: " + globalState + " / Local: " + localState;
139: }
140:
141: //
142: private void setGlobalState(double value) {
143: globalState = value;
144:
145: if (globalProgress != null) {
146: globalProgress.setValue((int) (10 * globalState));
147: }
148:
149: //
150: setChanged();
151: notifyObservers();
152: }
153:
154: private void setLocalState(double value) {
155: localState = value;
156:
157: if (localProgress != null) {
158: if (value == -1) {
159: localProgress.setIndeterminate(true);
160: } else {
161: localProgress.setIndeterminate(false);
162: localProgress.setValue((int) (10 * localState));
163: }
164: }
165:
166: //
167: setChanged();
168: notifyObservers();
169: }
170:
171: //
172: public JProgressBar getGlobalProgressBar() {
173: if (globalProgress == null) {
174: globalProgress = new JProgressBar(0, 1000);
175: setGlobalState(globalState);
176: }
177:
178: return globalProgress;
179: }
180:
181: public JProgressBar getLocalProgressBar() {
182: if (localProgress == null) {
183: localProgress = new JProgressBar(0, 1000);
184: setLocalState(localState);
185: }
186:
187: return localProgress;
188: }
189: }
|