source: tmcsimulator/trunk/src/tmcsim/client/cadscreens/view/CADCommandLineView.java @ 2

Revision 2, 13.4 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1package tmcsim.client.cadscreens.view;
2
3import java.awt.Color;
4import java.util.logging.Level;
5import java.util.logging.Logger;
6
7import javax.swing.JTextPane;
8import javax.swing.text.BadLocationException;
9import javax.swing.text.Document;
10import javax.swing.text.Style;
11import javax.swing.text.StyleConstants;
12import javax.swing.text.StyleContext;
13
14import tmcsim.client.CADCaret;
15import tmcsim.common.CADEnums.ARROW;
16import tmcsim.common.CADEnums.TEXT_STYLES;
17
18
19/**
20 * CADCommandLineView is the view class used in the CAD Client to
21 * display the current command line and process key presses.
22 * As characters are added and removed from the command line, the current
23 * text is contained within the commandLineText StringBuffer object.  The
24 * Document object is used to display the current command line text to the user.
25 * A position counter for the Caret is used to keep track of where input
26 * is to be received and characters are to be removed.
27 *
28 * @author Matthew Cechini
29 * @version
30 */
31@SuppressWarnings("serial")
32public class CADCommandLineView {
33   
34    /** Error Logger. */
35    private static Logger viewLogger = Logger.getLogger("tmcsim.client.cadscreens");
36
37    /** Font size for text displayed on CAD Screen */
38    private static int FONT_SIZE = 15;
39   
40    /** Document displaying CAD Screen text. */
41    private Document  theDoc    = null;
42
43    /** JTextPane displaying CAD Screen text. */
44    private JTextPane stylePane = null;
45   
46    /** Current position for text input. */
47    private int currentPosition;
48   
49    /** Current command line text. */
50    private StringBuffer commandLineText;   
51   
52    /** Caret used visually to track the current position of command line input. */
53    private CADCaret theCADCaret = null;
54   
55
56    /**
57     * Constructor.  Initialize data objects and styles.  Create the
58     * CADCaret and add it to the parameter JTextPane.
59     *
60     * @param textPane Target JTextPane whose Document object
61     * will be written to for command line display.
62     */
63    public CADCommandLineView(JTextPane textPane) {
64           
65        currentPosition = 0;
66        commandLineText = new StringBuffer(160);
67       
68        theCADCaret = new CADCaret();
69       
70        textPane.setCaret(theCADCaret);
71        textPane.setCaretColor(Color.yellow);
72       
73        theDoc = textPane.getDocument();   
74        initStyles();   
75       
76        clearCommandLine();
77        theCADCaret.setVisible(true);
78    }
79
80 
81    /**
82     * Sets the current command line with a new string.  Clear
83     * the current command line and then call the receiveKeyPress()
84     * method with each character in the new string.  The Caret
85     * is hidden during these operations to reduce erratic scroling.
86     *
87     * @param cmdLine New command line text.
88     */
89    public void setCommandLine(String cmdLine) 
90    {
91        try
92        {
93            clearCommandLine();   
94   
95            theCADCaret.setVisible(false);
96            for(int i = 0; i < cmdLine.length(); i++) 
97                receiveKeyPress(cmdLine.charAt(i)); 
98            theCADCaret.setVisible(true);
99        }
100        catch (Exception e)
101        {
102            viewLogger.log(Level.SEVERE, "Exception occured while setting command line.", e);
103        }
104    } 
105   
106    /**
107     * Replaces the character at the curent command line position with an
108     * empty space if the command line contains text. Decrement the position
109     * counter and move the caret back one space.
110     */
111    public void backspace() {
112           
113        try{
114            if(commandLineText.length() > 0 && currentPosition > 0) {
115                commandLineText.deleteCharAt(currentPosition-1);
116               
117                theDoc.remove(currentPosition-1, 1);
118               
119                if(currentPosition-1 == commandLineText.length()) {
120                    theDoc.insertString(currentPosition-1,
121                            " ",
122                            stylePane.getStyle(TEXT_STYLES.YELLOW.style));
123                }
124                else {
125                    theDoc.insertString(commandLineText.length(),
126                            " ",
127                            stylePane.getStyle(TEXT_STYLES.YELLOW.style));
128                }
129
130                currentPosition--;
131                theCADCaret.moveCaretBackward(1);
132            }
133           
134        } catch (BadLocationException ble) {
135            viewLogger.log(Level.SEVERE, "Exception in updating view document.", ble);
136        }       
137    }
138   
139
140    /**
141     * Receive a new key press from user.  If the current position is at least
142     * one character before the end of the current commmand line text, replace
143     * the current character with the new character and move the caret forward
144     * one space.  If not, the current position is at the end of the command
145     * line.  If the command line is not full, add the new character at the
146     * end of the command line and move the caret forward one space.  Else the
147     * command line is full, replace the last character with the new character
148     * and do not move the caret.
149     *
150     * @param inputChar Character being added to command line     
151     */
152    public void receiveKeyPress(char inputChar) {
153       
154        try {
155            //doesn't matter where we are, replace
156            if(currentPosition <= commandLineText.length() - 1 &&
157               commandLineText.length() != 0) 
158            {
159               
160                commandLineText.setCharAt(currentPosition, inputChar);             
161                theDoc.remove(currentPosition, 1);
162                theDoc.insertString(currentPosition,
163                                           String.valueOf(inputChar),
164                                           stylePane.getStyle(TEXT_STYLES.YELLOW.style));
165                currentPosition++;
166                theCADCaret.moveCaretForward(1);
167               
168            }
169            //else we are appending, be sure that the StringBuffer isn't full
170            else if(commandLineText.length() < 160) 
171            {
172                commandLineText.append(inputChar);
173               
174                theDoc.remove(currentPosition, 1);
175                theDoc.insertString(currentPosition,
176                                    String.valueOf(inputChar),
177                                    stylePane.getStyle(TEXT_STYLES.YELLOW.style));             
178                currentPosition = commandLineText.length(); 
179                theCADCaret.moveCaretForward(1);                               
180               
181            }
182            else if(commandLineText.length() == 160) 
183            {
184                commandLineText.setCharAt(currentPosition-1, inputChar);   
185                theDoc.remove(currentPosition-1, 1);
186                theDoc.insertString(currentPosition-1,
187                                    String.valueOf(inputChar),
188                                    stylePane.getStyle(TEXT_STYLES.YELLOW.style));
189            }
190           
191        } catch (BadLocationException ble) {
192            viewLogger.log(Level.SEVERE, "Exception in updating view document.", ble);
193        }       
194    }
195   
196    /**
197     * Receive an arrow press by the user. The following actions are taken
198     * for each direction:<br><br>
199     * <br>
200     *<table cellpadding="2" cellspacing="2" border="1"
201     * style="text-align: left; width: 250px;">
202     *  <tbody>
203     *    <tr>
204     *      <th>Direction<br></th>
205     *      <th>Action Taken<br></th>
206     *    </tr>
207     *    <tr>
208     *      <td>LEFT<br></td>
209     *      <td>If command line position is not at the beginning of the command
210     *          line, decrement the position and move the Caret back one space.</td>
211     *    </tr>
212     *    <tr>
213     *      <td>UP<br></td>
214     *      <td>If the command line position is at the end of a full(160 character)
215     *          command, move back 81 characters to the end of the previous line. 
216     *          If the command line is not full, but the position is on the second
217     *          line, move back 80 characters.  Move the Caret back the same
218     *          number of spaces.  (The descrepancy in moving 81 or
219     *          80 charactes has to do with the value of the position counter
220     *          when it is at the end of a full command line)</td>
221     *    </tr>
222     *    <tr>
223     *      <td>RIGHT</td>
224     *      <td>If command line position is not at the end of the command line,
225     *          increment the position and move the Caret foward one space.</td>
226     *    </tr>
227     *    <tr>
228     *      <td>DOWN</td>
229     *      <td>If the command line position is on the first line of the command
230     *          line, and is directly above or to the left of the last character
231     *          on the second line, move forward 80 characters.  Else if the
232     *          position is on the first line and there is a second line, move the
233     *          position to the end of the command line.  Move the Caret forward
234     *          the same number of spaces.</td>
235     *    </tr>
236     *  </tbody>
237     *</table>
238     *
239     * @param direction Arrow direction. 
240     */
241    public void receiveArrow(ARROW direction) {
242
243        switch(direction) {
244           
245            case LEFT:
246                if(currentPosition > 0) { 
247                    currentPosition--;
248                    theCADCaret.moveCaretBackward(1);   
249                }
250                break;
251                   
252            case UP:
253                if(currentPosition == 160) {
254                    currentPosition -= 81;
255                    theCADCaret.moveCaretBackward(81);   
256                }
257                else if(currentPosition > 80) {
258                    currentPosition -= 80;
259                    theCADCaret.moveCaretBackward(80);   
260                }
261                break;
262               
263            case RIGHT:
264                if(currentPosition < commandLineText.length() - 1) {
265                    currentPosition++;
266                    theCADCaret.moveCaretForward(1);
267                }
268                break;
269               
270            case DOWN:
271                if(currentPosition < (commandLineText.length() - 80)) {
272                    currentPosition += 80;
273                    theCADCaret.moveCaretForward(80);
274                }
275                else if (currentPosition <= 80 && 
276                         commandLineText.length() <= currentPosition + 80) {
277
278                    theCADCaret.moveCaretForward(commandLineText.length() - 
279                            currentPosition);
280                    currentPosition = commandLineText.length();
281                }
282                break;
283           
284        }     
285    }
286
287
288    /**
289     * Refresh the command line by saving the text, clearing the current
290     * command line, and then calling the receiveKeyPress() method for all
291     * characters in the saved text.  The Caret is hidden during these
292     * operations to eliminate erratic scrolling.
293     */
294    public void refreshView() {
295           
296        String savedCmdLine = commandLineText.toString();
297       
298        clearCommandLine();
299
300        theCADCaret.setVisible(false);
301
302        for(int pos = 0; pos < savedCmdLine.length(); pos++) {
303            receiveKeyPress(savedCmdLine.charAt(pos));
304        }
305        theCADCaret.setVisible(true);
306    }
307
308    /**
309     * Get the current command line string.
310     * @return Command line string.
311     */
312    public String getCommandLine() {
313        return commandLineText.toString(); 
314    } 
315   
316   
317    /**
318     * Clears the command line by clearing the command line StringBuffer,
319     * clearing the command line document, resetting the position count, and
320     * resetting the Caret. The Caret is hidden during these operations to
321     * eliminate erratic scrolling.
322     */
323    public void clearCommandLine() 
324    {
325   
326        try 
327        {
328            theCADCaret.setVisible(false);
329            currentPosition = 0;
330            commandLineText.setLength(0);
331           
332            while(theDoc.getLength() > 0) 
333            {
334                theDoc.remove(0, 1);
335            }       
336           
337            for(int i = 0; i < 160; i++) 
338            {
339                theDoc.insertString(i,
340                        " ",
341                        stylePane.getStyle(TEXT_STYLES.YELLOW.style));
342            }
343
344            theCADCaret.resetCaretPosition();
345            theCADCaret.setVisible(true);
346        } 
347        catch (Exception e) 
348        {
349            viewLogger.log(Level.SEVERE, "Exception in updating view document.", e);
350        }           
351       
352    }   
353   
354
355    /**
356     * This method intializes the stylePane with the styles found in the
357     * TEXT_STYLES enumeration that are needed for command line display.
358     * The value of the static FONT_SIZE is used for text sizing, along
359     * with the COURIER font style.
360     */
361    private void initStyles() {
362       
363        stylePane = new JTextPane();
364       
365        Style def = StyleContext.getDefaultStyleContext().
366                                        getStyle(StyleContext.DEFAULT_STYLE);
367                                       
368        Style regular = stylePane.addStyle(TEXT_STYLES.REGULAR.style, def);
369        StyleConstants.setFontFamily(def, TEXT_STYLES.COURIER.style);       
370       
371        Style s = stylePane.addStyle(TEXT_STYLES.YELLOW.style, regular);
372        StyleConstants.ColorConstants.setForeground(s, Color.yellow);           
373        StyleConstants.setFontSize(s, FONT_SIZE); 
374
375        s = stylePane.addStyle(TEXT_STYLES.GREEN.style, regular);
376        StyleConstants.ColorConstants.setForeground(s, Color.green);           
377        StyleConstants.setFontSize(s, FONT_SIZE);           
378    }
379   
380}
Note: See TracBrowser for help on using the repository browser.