source: trunk/src/org/expeditee/items/widgets/SampledHDWidget1.java@ 1398

Last change on this file since 1398 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.7 KB
Line 
1/**
2 * SampledHDWidget1.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items.widgets;
20
21import java.awt.FlowLayout;
22import java.util.Random;
23
24import javax.swing.JButton;
25import javax.swing.JComboBox;
26import javax.swing.JLabel;
27import javax.swing.JPanel;
28
29import org.expeditee.items.Text;
30
31public class SampledHDWidget1 extends HeavyDutyInteractiveWidget {
32
33 private boolean isIndeterminant = false;
34 private Object loadInterrupter = new Object();
35
36 public SampledHDWidget1(Text source, String[] args) {
37 super(source, new JPanel(new FlowLayout()), 50, 600, 50, 400, 1);
38
39 JPanel p = (JPanel)super._swingComponent;
40
41 p.add(new JButton("Click me"));
42 p.add(new JButton("Click me # 2"));
43 p.add(new JLabel("I am a label"));
44 p.add(new JComboBox(new String[] {"dog", "fish", "cat", "pig"}));
45
46 if (args != null) {
47 for (String arg : args) {
48 if (arg != null && arg.equalsIgnoreCase("indi")) {
49 isIndeterminant = true;
50 break;
51 }
52 }
53 }
54 }
55
56 @Override
57 protected String[] getArgs() {
58 if (isIndeterminant) return new String[] {"indi"};
59 return null;
60 }
61
62 @Override
63 protected void cancelLoadWidgetData() {
64 System.out.println(getClass().getName() + ":cancelLoadWidgetData");
65 synchronized(loadInterrupter) {
66 loadInterrupter.notify();
67 }
68
69 }
70
71 @Override
72 protected float loadWidgetData() {
73
74 Random rand = new Random();
75 rand.setSeed(System.currentTimeMillis());
76
77 // fail some of them
78 if ((rand.nextInt() % 4) == 0) {
79 setLoadScreenMessage("Failed to load metadata");
80 return LOAD_STATE_FAILED;
81 }
82
83 setLoadScreenMessage("Resolving paths...");
84
85 if (isIndeterminant) updateLoadPercentage(-1.0f);
86
87 int totalLoadTime = 2000 + (Math.abs(rand.nextInt()) % 10000);
88 int loadTimeLeft = totalLoadTime;
89
90 while (loadTimeLeft > 0) {
91 int waitTime = 100 + (Math.abs(rand.nextInt()) % 2000);
92
93 try {
94 synchronized(loadInterrupter) {
95 loadInterrupter.wait(waitTime);
96 }
97 } catch (InterruptedException e) { /* Consume */ }
98
99 if (hasCancelBeenRequested()) {
100 // Release resources
101 System.out.println(getClass().getName() + ":INTERUPTED LOAD - EXITING");
102 return LOAD_STATE_INCOMPLETED;
103 }
104
105 float perc = ((float)(totalLoadTime - loadTimeLeft)) / ((float)totalLoadTime);
106 if (perc <= 0.0f) perc = 0.01f;
107 updateLoadPercentage(perc);
108
109 if (perc > 0.4f && perc < 1.0f) {
110 setLoadScreenMessage("Loading metadata...");
111 }
112
113 loadTimeLeft -= waitTime;
114 }
115
116 return LOAD_STATE_COMPLETED;
117 }
118
119 public int getLoadDelayTime() {
120 return 1000;
121 }
122
123 public boolean doesNeedSaving() {
124 return false;
125 }
126
127 public String getSaveName() {
128 return "";
129 }
130
131 @Override
132 protected void saveWidgetData() {
133 System.out.println(getClass().getName() + ":saveWidgetData");
134 }
135
136 @Override
137 protected void unloadWidgetData() {
138 System.out.println(getClass().getName() + ":unloadWidgetData");
139 }
140
141 @Override
142 protected void tempUnloadWidgetData() {
143 System.out.println(getClass().getName() + ":tempUnloadWidgetData");
144 }
145
146
147}
Note: See TracBrowser for help on using the repository browser.