Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ServerFrame.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.org.ownage.dmdirc.ui;
  23. import java.awt.Dimension;
  24. import java.util.Date;
  25. import javax.swing.JScrollBar;
  26. import uk.org.ownage.dmdirc.Server;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.beans.PropertyChangeEvent;
  30. import java.beans.PropertyChangeListener;
  31. import javax.swing.border.Border;
  32. import javax.swing.border.EmptyBorder;
  33. import javax.swing.plaf.basic.BasicInternalFrameUI;
  34. import uk.org.ownage.dmdirc.commandparser.CommandParser;
  35. import uk.org.ownage.dmdirc.commandparser.CommandWindow;
  36. import uk.org.ownage.dmdirc.logger.ErrorLevel;
  37. import uk.org.ownage.dmdirc.logger.Logger;
  38. import uk.org.ownage.dmdirc.ui.messages.Formatter;
  39. import uk.org.ownage.dmdirc.ui.messages.Styliser;
  40. /**
  41. * The ServerFrame is the MDI window that shows server messages to the user
  42. * @author chris
  43. */
  44. public class ServerFrame extends javax.swing.JInternalFrame implements CommandWindow {
  45. /**
  46. * A version number for this class. It should be changed whenever the class
  47. * structure is changed (or anything else that would prevent serialized
  48. * objects being unserialized with the new class).
  49. */
  50. private static final long serialVersionUID = 1;
  51. /**
  52. * The border used when the frame is not maximised
  53. */
  54. private Border myborder;
  55. /**
  56. * The dimensions of the titlebar of the frame
  57. **/
  58. private Dimension titlebarSize;
  59. /**
  60. * whether to auto scroll the textarea when adding text
  61. */
  62. private boolean autoScroll = true;
  63. /**
  64. * holds the scrollbar for the frame
  65. */
  66. private JScrollBar scrollBar;
  67. private CommandParser commandParser;
  68. /**
  69. * Creates a new ServerFrame
  70. * @param commandParser The command parser to use
  71. */
  72. public ServerFrame(CommandParser commandParser) {
  73. initComponents();
  74. setFrameIcon(MainFrame.getMainFrame().getIcon());
  75. setMaximizable(true);
  76. setClosable(true);
  77. setResizable(true);
  78. scrollBar = jScrollPane1.getVerticalScrollBar();
  79. this.commandParser = commandParser;
  80. jTextField1.addActionListener(new ActionListener() {
  81. public void actionPerformed(ActionEvent actionEvent) {
  82. try {
  83. ServerFrame.this.commandParser.parseCommand(ServerFrame.this, jTextField1.getText());
  84. } catch (Exception e) {
  85. Logger.error(ErrorLevel.ERROR, e);
  86. }
  87. jTextField1.setText("");
  88. }
  89. });
  90. addPropertyChangeListener("maximum", new PropertyChangeListener() {
  91. public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
  92. if (propertyChangeEvent.getNewValue().equals(Boolean.TRUE)) {
  93. ServerFrame.this.myborder = getBorder();
  94. ServerFrame.this.titlebarSize =
  95. ((BasicInternalFrameUI)getUI())
  96. .getNorthPane().getPreferredSize();
  97. ((BasicInternalFrameUI)getUI()).getNorthPane()
  98. .setPreferredSize(new Dimension(0,0));
  99. setBorder(new EmptyBorder(0,0,0,0));
  100. MainFrame.getMainFrame().setMaximised(true);
  101. } else {
  102. autoScroll = ((scrollBar.getValue() + scrollBar.getVisibleAmount())
  103. != scrollBar.getMaximum());
  104. if(autoScroll) {
  105. jTextPane1.setCaretPosition(jTextPane1.getStyledDocument().getLength());
  106. }
  107. setBorder(ServerFrame.this.myborder);
  108. ((BasicInternalFrameUI)getUI()).getNorthPane()
  109. .setPreferredSize(ServerFrame.this.titlebarSize);
  110. MainFrame.getMainFrame().setMaximised(false);
  111. }
  112. }
  113. });
  114. }
  115. /**
  116. * Makes this frame visible. We don't call this from the constructor
  117. * so that we can register an actionlistener for the open event before
  118. * the frame is opened.
  119. */
  120. public void open() {
  121. setVisible(true);
  122. }
  123. /**
  124. * Adds a line of text to the main text area, and scrolls the text pane
  125. * down so that it's visible if the scrollbar is already at the bottom
  126. * @param line text to add
  127. */
  128. public void addLine(String line) {
  129. String ts = Formatter.formatMessage("timestamp", new Date());
  130. Styliser.addStyledString(jTextPane1.getStyledDocument(), ts);
  131. Styliser.addStyledString(jTextPane1.getStyledDocument(), line+"\n");
  132. autoScroll = ((scrollBar.getValue() + scrollBar.getVisibleAmount())
  133. != scrollBar.getMaximum());
  134. if(autoScroll) {
  135. jTextPane1.setCaretPosition(jTextPane1.getStyledDocument().getLength());
  136. }
  137. }
  138. /**
  139. * Formats the arguments using the Formatter, then adds the result to the
  140. * main text area
  141. * @param messageType The type of this message
  142. * @param args The arguments for the message
  143. */
  144. public void addLine(String messageType, Object... args) {
  145. addLine(Formatter.formatMessage(messageType, args));
  146. }
  147. /** This method is called from within the constructor to
  148. * initialize the form.
  149. * WARNING: Do NOT modify this code. The content of this method is
  150. * always regenerated by the Form Editor.
  151. */
  152. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  153. private void initComponents() {
  154. jTextField1 = new javax.swing.JTextField();
  155. jScrollPane1 = new javax.swing.JScrollPane();
  156. jTextPane1 = new javax.swing.JTextPane();
  157. setTitle("Server Frame");
  158. jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  159. jTextPane1.setEditable(false);
  160. jScrollPane1.setViewportView(jTextPane1);
  161. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  162. getContentPane().setLayout(layout);
  163. layout.setHorizontalGroup(
  164. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  165. .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
  166. .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
  167. );
  168. layout.setVerticalGroup(
  169. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  170. .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  171. .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
  172. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  173. .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  174. );
  175. pack();
  176. }// </editor-fold>//GEN-END:initComponents
  177. // Variables declaration - do not modify//GEN-BEGIN:variables
  178. private javax.swing.JScrollPane jScrollPane1;
  179. private javax.swing.JTextField jTextField1;
  180. private javax.swing.JTextPane jTextPane1;
  181. // End of variables declaration//GEN-END:variables
  182. }