summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/gui/botConsole/BotConsoleFrame.java
blob: ba7dc8e28039ccfd6b0dfc3c22393cd1815d006a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * !!!!!
 * NOTE: PLEASE ONLY EDIT THIS USING THE NETBEANS IDE 6.0.1 OR HIGHER!!!!
 * !!!!!
 * 
 * ... an .xml file is associated with this class. Cheers.
 *
 * BotConsoleFrame.java
 * 
 * Created on 28 March 2008, 08:35
 */

package org.reprap.gui.botConsole;

import org.reprap.Preferences;
import org.reprap.utilities.Debug;

import javax.swing.JOptionPane;
//import org.reprap.machines.MachineFactory;

/**
 *
 * @author  Ed Sells, March 2008
 * 
 * Console to operate the RepRap printer manually.
 * 
 */
public class BotConsoleFrame extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;

    private Thread pollThread = null;
    private boolean carryOnPolling = true;
    private GenericExtruderTabPanel[] extruderPanels;
    double fractionDone = -1;
    private static BotConsoleFrame bcf = null;
    private static int exPanelNumber;
    
    /** Creates new form BotConsoleFrame */
    public BotConsoleFrame() {
        try {
            checkPrefs();
        }
        catch (Exception e) {
            System.err.println("Failure trying to initialise comms in botConsole: " + e);
            JOptionPane.showMessageDialog(null, e.getMessage());
            return;
        }

        initComponents();

        this.setTitle("RepRap Console");

        
        /*
         * Fork off a thread to keep the panels up-to-date
         */
        exPanelNumber = 0;
        pollThread = new Thread() 
        {
        	public void run() 
        	{
        		Thread.currentThread().setName("GUI Poll");
        		while(true)
        		{
        			try 
        			{
        				Thread.sleep(1500);
        				updateProgress();
        				if(carryOnPolling)
        					updatePanels();   
        			} catch (InterruptedException ex) 
        			{
        				// This is normal when shutting down, so ignore		
        			}
        		}
        	}
        };

        pollThread.start(); 
    }
    

    
    /**
     * The update thread calls this to update everything
     * that relies on information from the RepRap machine.
     */
    private void updatePanels()
    {
    	int currentExtruder = org.reprap.Main.gui.getPrinter().getExtruder().getID();
    	
    	org.reprap.Main.gui.getPrinter().selectExtruder(exPanelNumber);
    	extruderPanels[exPanelNumber].refreshTemperature();
    	org.reprap.Main.gui.getPrinter().selectExtruder(currentExtruder);
    	
    	exPanelNumber++;
    	if(exPanelNumber >= extruderPanels.length)
    		exPanelNumber = 0;   		
    }
    
    /**
     * The update thread calls this to update everything
     * that is independent of the RepRap machine.
     * @param fractionDone
     */
    private void updateProgress()
    {
    	printTabFrame1.updateProgress(fractionDone);
    }
    
    public void setFractionDone(double f)
    {
    	fractionDone = f;
    }

    /**
     * "Suspend" and "resume" the poll thread.
     * We don't use the actual suspend call (depricated anyway) to
     * prevent resource locking.
     *
     */
    public void suspendPolling()
    {
    	carryOnPolling = false;
    	try 
		{
			Thread.sleep(200); 
		} catch (InterruptedException ex) 
		{
			// This is normal when shutting down, so ignore		
		}
    }
    public void resumePolling()
    {
    	try 
		{
			Thread.sleep(200); 
		} catch (InterruptedException ex) 
		{
			// This is normal when shutting down, so ignore		
		}
    	carryOnPolling = true;
    }
 
    
    private void checkPrefs() throws Exception {
        
        // ID the number of extruder
        extruderCount = Preferences.loadGlobalInt("NumberOfExtruders");
        if (extruderCount < 1)
            throw new Exception("A Reprap printer must contain at least one extruder");
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        initialiseExtruderPanels();
        printTabFrame1 = new org.reprap.gui.botConsole.PrintTabFrame();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTabbedPane1.setRequestFocusEnabled(false);
        jTabbedPane1.addTab("Print", printTabFrame1);

        addExtruderPanels();

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 750, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(5, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 400, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(5, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                bcf = new BotConsoleFrame();
                bcf.setVisible(true);
                bcf.printTabFrame1.setConsoleFrame(bcf);
                bcf.xYZTabPanel.setConsoleFrame(bcf);
                for(int i = 0; i < bcf.extruderPanels.length; i++)
                	bcf.extruderPanels[i].setConsoleFrame(bcf);
            }
            
        });
     }
    
    public static BotConsoleFrame getBotConsoleFrame()
    {
    	return bcf;
    }
//    
//    public static org.reprap.gui.botConsole.XYZTabPanel XYZ()
//    {
//    	return bcf.xYZTabPanel;
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.reprap.gui.botConsole.PrintTabFrame printTabFrame1;
    // End of variables declaration//GEN-END:variables
    
    private org.reprap.gui.botConsole.XYZTabPanel xYZTabPanel;
    
 //   private static int motorID = 0;

    
//    public static int getMotorID() {
//        motorID++;
//        return motorID;
//    }
    
    public static PrintTabFrame getPrintTabFrame()
    {
    	return bcf.printTabFrame1;
    }
    
    public static XYZTabPanel getXYZTabPanel()
    {
    	return bcf.xYZTabPanel;
    }  
    
    public static GenericExtruderTabPanel getGenericExtruderTabPanel(int i)
    {
    	if(i >= 0 && i < bcf.extruderPanels.length)
    		return bcf.extruderPanels[i];
    	System.err.println("getGenericExtruderTabPanel - extruder out of range: " + i);
    	return bcf.extruderPanels[0];
    }
    
    private void initialiseExtruderPanels() {

        extruderPanels = new GenericExtruderTabPanel[extruderCount];
        for (int i = 0; i < extruderCount; i++) {
            extruderPanels[i] = new GenericExtruderTabPanel();
            try {
                extruderPanels[i].initialiseExtruders(i);
            }
            catch (Exception e) {
                System.out.println("Failure trying to initialise extruders in botConsole: " + e);
                JOptionPane.showMessageDialog(null, e.getMessage());
                return;
            }            
            try {
                extruderPanels[i].setPrefs();
            }
            catch (Exception e) {
                System.out.println("Problem loading prefs for Extruder " + i);
                JOptionPane.showMessageDialog(null, "Problem loading prefs for Extruder " + i);
            }
        }
    }
    
    private void addExtruderPanels() {
        
        xYZTabPanel = new org.reprap.gui.botConsole.XYZTabPanel();

        jTabbedPane1.addTab("XYZ", xYZTabPanel);
        for (int i = 0; i < extruderCount; i++) {
            jTabbedPane1.addTab("Extruder " + i, extruderPanels[i]);
        }
        pack();
    }
    



    private int extruderCount;
    //private int currentExtruder;
    

		
    
}