Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/client/cadscreens/view/CADMainView.java: ("Can't find a temporary directory: Internal error", 20014)

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

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

Initial Import of project files

RevLine 
1package tmcsim.client.cadscreens.view;
2
3import java.awt.Color;
4import java.util.Observable;
5import java.util.Vector;
6import java.util.logging.Level;
7import java.util.logging.Logger;
8
9import javax.swing.JTextPane;
10import javax.swing.text.BadLocationException;
11import javax.swing.text.Document;
12import javax.swing.text.Style;
13import javax.swing.text.StyleConstants;
14import javax.swing.text.StyleContext;
15
16import tmcsim.common.CADEnums.TEXT_STYLES;
17
18
19/**
20 * The CADMainView is the base view class for all CAD Screens.  The
21 * Vector of CADDocElement objects contains all text/style pairs
22 * that will be used in creating a document for display.  Within the list of
23 * CADDocElements are separate "pages."  CADDocElements that contain an endline
24 * character signify the end of a line.  Each CADMainView "page" is a collection
25 * of these lines.  The static MAIN_CAD_SCREEN_LINES defines how many lines are
26 * displayed on each page.  When the refreshView() method is called, the
27 * text/style pairs for the current page are added to the local Document object.
28 * The CADMainView keeps track of the current page, total number of pages, current
29 * number lines on the page displayed, and the total number of lines. 
30 *
31 * @author
32 * @version
33 */
34public class CADMainView extends Observable {
35   
36    /** Error Logger.  */
37    private static Logger viewLogger = Logger.getLogger("tmcsim.client.cadscreens");
38   
39    /** Number of viewable lines on a CAD Screen page.  */
40    protected final static int MAIN_CAD_SCREEN_LINES = 20;
41   
42    /** Font size for text displayed on CAD Screen */
43    private final static int FONT_SIZE = 15;   
44   
45    /** Vector of Document elements used to create the display. */
46    private Vector<CADDocElement> theDocElements;
47   
48    /** Document displaying CAD Screen text. */
49    private Document  theDoc    = null;
50
51    /** JTextPane displaying CAD Screen text. */
52    private JTextPane stylePane = null;
53           
54    /** Current page number being viewed in this screen. (# => 1)*/
55    private int currentPage;
56   
57    /** Total number of pages for this screen. (# => 1)*/
58    private int totalPages; 
59   
60    /** Total number of lines on all pages for this screen. (# => 0)*/
61    private int totalLines;
62   
63    /** The number of lines displayed on the last page for this screen. (# => 0) */
64    private int lastPageLineCount;
65   
66
67    /**
68     * Constructor.  Initialize data objects and styles.
69     *
70     * @param viewDoc Target Document for text display.
71     */
72    public CADMainView(Document viewDoc) {
73       
74        theDocElements    = new Vector<CADDocElement>(); 
75       
76        currentPage       = 1;
77        totalPages        = 1;     
78        totalLines        = 0;
79        lastPageLineCount = 0;     
80       
81        theDoc = viewDoc;
82        initStyles();
83       
84    }
85   
86    /**
87     * Method pads the parameter string with spaces to the right
88     * of the string until the string length is equal to the parameter
89     * length.  If the parameter width is less than the length of the
90     * parameter String, no action is taken.
91     *
92     * @param str String to pad.
93     * @param width Desired string length.
94     * @return Padded string.
95     */
96    protected String rPad(String str, int width) 
97    {
98        StringBuffer buf = new StringBuffer(str);
99        while(buf.length() < width)
100            buf.append(" ");
101       
102        return buf.toString();     
103    }
104   
105    /**
106     * Method pads the parameter string with spaces to the left
107     * of the string until the string length is equal to the parameter
108     * length.  If the parameter width is less than the length of the
109     * parameter String, no action is taken.
110     *
111     * @param str String to pad.
112     * @param width Desired string length.
113     * @return Padded string.
114     */   
115    protected String lPad(String str, int width) 
116    {
117        StringBuffer buf = new StringBuffer(str);
118        while(buf.length() < width)
119            buf.insert(0, " ");
120       
121        return buf.toString();     
122    }
123       
124   
125    /**
126     * Adds a new string to the list of format strings.  The parameter string
127     * is tokenized by '\n' characters.  Each token is considered a new
128     * "line" of text.  Strings without an endline character will be treated
129     * as a text chunk.  Each new line or text chunk is added to the document
130     * with the associated parameter text style.  For every new "line"
131     * being added, the lastPageLineCount is incremented. While the
132     * lastPageLineCount is < the maximum number of MAIN_CAD_SCREEN_LINES,
133     * increment it.  When this becomes false, reset the lastPageLineCount
134     * to 1 and increment the totalPages count.
135     *
136     * @param text Text string to add to the screen document
137     * @param style Text style to associate with the text being added.
138     */
139    protected void addDocElement(String text, TEXT_STYLES style) {
140       
141        int beginPos = 0;
142        int endlPos  = text.indexOf('\n', 0);
143       
144        while(endlPos != -1) {
145           
146            //if current lines less than max per page, add line, else add new page
147            if(lastPageLineCount < MAIN_CAD_SCREEN_LINES) 
148                lastPageLineCount++;
149            else {
150                lastPageLineCount = 1;
151                totalPages++;
152            }           
153           
154            totalLines++; //keep track of how many total lines have been added
155            endlPos++;    //include the endline character
156            theDocElements.add(new CADDocElement(text.substring(beginPos, endlPos), style));
157           
158            beginPos = endlPos; 
159            endlPos  = text.indexOf('\n', endlPos);
160        }
161       
162        if(beginPos != text.length()) {
163            theDocElements.add(new CADDocElement(text.substring(beginPos, text.length()), style));
164        }
165    }
166   
167    /**
168     * This method is used to move the screen's displayed information up one
169     * page.  If the current page is greater than one, decrement the
170     * currentPage variable and refresh the view.
171     */
172    public void pageUp() {
173        if(currentPage > 1) {
174            currentPage--;
175           
176            try {
177                refreshView(currentPage);
178            } catch (Exception e) {
179                // due to conditional statement, parameter will never be
180                // invalid.
181            }
182        }
183    }   
184
185    /**
186     * This method is used to move the screen's displayed information down one
187     * page. If the current page is less than the total number of pages,
188     * increment the currentPage variable and refresh the view.
189     */
190    public void pageDown() {
191        if (currentPage < totalPages) {
192            currentPage++;
193
194            try {
195                refreshView(currentPage);
196            } catch (Exception e) {
197                // due to conditional statement, parameter will never be
198                // invalid.
199            }
200        }
201    }   
202   
203    /**
204     * Get the current page number.
205     *
206     * @return Current page number. (# >= 1)
207     */
208    public int getCurrentPage() {
209        return currentPage;
210    }
211   
212    /**
213     * This method refreshes the screen to a specific page, passed in as a
214     * parameter.  The current document is cleared and the page scrolling
215     * text is updated according the new page position on the screen.
216     * The document element index for the first line of the new page is found.
217     * All text and style pairs in the document elements for the new page are
218     * added to the Document object.  Each pair that is added is checked
219     * for endline character, signifying a new line.  This process ends when
220     * enough pairs have been added to display the maximum number of lines on
221     * the screen, or there are no more document elements.
222     *
223     * @param gotoPage Page number to display.  (# >= 1)
224     */
225   public void refreshView(int gotoPage) {
226           
227       if(gotoPage > totalPages) {
228           currentPage = totalPages;
229       }
230       else {
231           currentPage = gotoPage;
232       }
233           
234        int currIndex = 0;
235        int lineCount = 0;
236       
237        try {
238            theDoc.remove(0, theDoc.getLength());
239
240            notifyPageScrolling();
241                       
242            //If the page being viewed is not the first page, search through
243            //the formatStrings vector until the first string of the current
244            //page is found.
245            if(currentPage != 1) {
246                while(currIndex < theDocElements.size() && 
247                      lineCount < ((currentPage - 1)*MAIN_CAD_SCREEN_LINES)) {
248                           
249                    lineCount += countEndLines(theDocElements.get(currIndex).text); 
250                   
251                    currIndex++;
252                }       
253            }               
254            //else this is the first page   
255            lineCount = 0;
256       
257            //always output the header bar
258            theDoc.insertString(theDoc.getLength(), 
259                                "==================================================" +
260                                "==============================" + "\n", 
261                                stylePane.getStyle(TEXT_STYLES.AQUA.style));
262                                 
263            //start outputting strings from the formatStrings vector, counting
264            //how many lines have been output                   
265            while(currIndex < theDocElements.size() && 
266                  lineCount < MAIN_CAD_SCREEN_LINES) {
267                   
268               theDoc.insertString(theDoc.getLength(), 
269                                   theDocElements.get(currIndex).text,
270                                   stylePane.getStyle(theDocElements.get(currIndex).style.style));             
271               
272               lineCount += countEndLines(theDocElements.get(currIndex).text);         
273               
274               currIndex++;                 
275            }
276        } 
277        catch (BadLocationException ble) {
278            viewLogger.logp(Level.SEVERE, "CADMainView", "refreshView()", 
279                    "Exception in updating view document.", ble);
280        }
281       
282    }
283   
284   /**
285    * This method counts the number of endline terminated strings contained
286    * within the parameter string.
287    *
288    * @param text A text string.
289    * @return The number of lines contained in the parameter text string.
290    */
291   private int countEndLines(String text) {
292       
293       int lastPos   = 0;
294       int endlCount = 0;
295       
296       while(text.indexOf("\n", lastPos) != -1) {
297           lastPos = text.indexOf("\n", lastPos) + 1;
298           endlCount++;
299       }
300       
301       return endlCount;
302   }
303   
304   /**
305    * This method determines whether the current CAD Screen can be paged up
306    * or down, then notifies observers with the associated scrolling string. 
307    * <br>
308    * The following are the possible outcomes:
309    *
310    * - Only page up          - "PGUP"
311    * - Only page down        - "PGDN"
312    * - Page up and page down - "UPDN"
313    * - No scrolling          - "    "
314    */
315   private void notifyPageScrolling() {
316       
317        String pageScrolling = "     ";   
318
319        if(totalLines < MAIN_CAD_SCREEN_LINES) {
320            //smile, only one page
321        }
322        else if((currentPage)*MAIN_CAD_SCREEN_LINES < totalLines &&
323                currentPage == 1) {
324            //pgdn
325            pageScrolling = "PG DN";
326        }
327        else if((currentPage)*MAIN_CAD_SCREEN_LINES < totalLines &&
328                currentPage > 1) {
329            //page up and down
330            pageScrolling = "UP DN";
331        }
332        else if(currentPage > 1) {
333            pageScrolling = "PG UP";
334        }
335       
336        setChanged();
337        notifyObservers(pageScrolling);
338               
339   }
340   
341   /**
342    * This method intializes the stylePane with the styles found in the
343    * TEXT_STYLES enumeration that are needed for main screen display.
344    * The value of the static FONT_SIZE is used for text sizing, along
345    * with the COURIER font style.
346    */
347    private void initStyles() {
348       
349        stylePane = new JTextPane();
350       
351        Style def = StyleContext.getDefaultStyleContext().
352                                    getStyle(StyleContext.DEFAULT_STYLE);
353
354        Style regular = stylePane.addStyle(TEXT_STYLES.REGULAR.style, def);
355        StyleConstants.setFontFamily(def, TEXT_STYLES.COURIER.style);
356
357        Style s = stylePane.addStyle(TEXT_STYLES.ITALIC.style, regular);
358        StyleConstants.setItalic(s, true);
359
360        s = stylePane.addStyle(TEXT_STYLES.BOLD.style, regular);
361        StyleConstants.setBold(s, true);
362       
363        s = stylePane.addStyle(TEXT_STYLES.BLUE.style, regular);
364        StyleConstants.ColorConstants.setForeground(s, Color.blue);
365        StyleConstants.setFontSize(s, FONT_SIZE);
366       
367        s = stylePane.addStyle(TEXT_STYLES.AQUA.style, regular);
368        StyleConstants.ColorConstants.setForeground(s, new Color(0,128,128));
369        StyleConstants.setFontSize(s, FONT_SIZE);               
370       
371        s = stylePane.addStyle(TEXT_STYLES.RED.style, regular);
372        StyleConstants.ColorConstants.setForeground(s, Color.red);           
373        StyleConstants.setFontSize(s, FONT_SIZE);
374       
375        s = stylePane.addStyle(TEXT_STYLES.GRAY.style, regular);
376        StyleConstants.ColorConstants.setForeground(s, Color.gray);           
377        StyleConstants.setFontSize(s, FONT_SIZE);
378       
379        s = stylePane.addStyle(TEXT_STYLES.CYAN.style, regular);
380        StyleConstants.ColorConstants.setForeground(s, Color.cyan);           
381        StyleConstants.setFontSize(s, FONT_SIZE);               
382
383        s = stylePane.addStyle(TEXT_STYLES.YELLOW.style, regular);
384        StyleConstants.ColorConstants.setForeground(s, Color.yellow);           
385        StyleConstants.setFontSize(s, FONT_SIZE); 
386
387        s = stylePane.addStyle(TEXT_STYLES.GREEN.style, regular);
388        StyleConstants.ColorConstants.setForeground(s, Color.green);           
389        StyleConstants.setFontSize(s, FONT_SIZE); 
390
391        s = stylePane.addStyle(TEXT_STYLES.ORANGE.style, regular);
392        StyleConstants.ColorConstants.setForeground(s, Color.orange);           
393        StyleConstants.setFontSize(s, FONT_SIZE);
394
395        s = stylePane.addStyle(TEXT_STYLES.REVERSE_CYAN.style, regular);
396        StyleConstants.ColorConstants.setForeground(s, Color.black);   
397        StyleConstants.ColorConstants.setBackground(s, Color.cyan);           
398        StyleConstants.setFontSize(s, FONT_SIZE);
399       
400    }           
401       
402   
403}
Note: See TracBrowser for help on using the repository browser.