Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MainFrame.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.Image;
  24. import java.awt.event.ItemEvent;
  25. import java.awt.event.ItemListener;
  26. import java.awt.event.WindowEvent;
  27. import java.awt.event.WindowListener;
  28. import java.beans.PropertyVetoException;
  29. import javax.swing.ImageIcon;
  30. import uk.org.ownage.dmdirc.*;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import javax.swing.JInternalFrame;
  34. import uk.org.ownage.dmdirc.logger.Logger;
  35. import uk.org.ownage.dmdirc.logger.ErrorLevel;
  36. /**
  37. * The main application frame
  38. * @author chris
  39. */
  40. public class MainFrame extends javax.swing.JFrame implements WindowListener {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /**
  48. * Singleton instance of MainFrame
  49. */
  50. private static MainFrame me;
  51. /**
  52. * Whether the internal frames are maximised or not
  53. */
  54. private boolean maximised = false;
  55. /**
  56. * The current number of pixels to displace new frames in the X direction
  57. */
  58. private int xOffset = 0;
  59. /**
  60. * The current number of pixels to displace new frames in the Y direction
  61. */
  62. private int yOffset = 0;
  63. /**
  64. * The main application icon
  65. */
  66. private ImageIcon imageIcon;
  67. /**
  68. * Returns the singleton instance of MainFrame
  69. * @return MainFrame instance
  70. */
  71. public static MainFrame getMainFrame() {
  72. if (me == null) {
  73. me = new MainFrame();
  74. }
  75. return me;
  76. }
  77. /** Creates new form MainFrame */
  78. public MainFrame() {
  79. initComponents();
  80. // Load an icon
  81. ClassLoader cldr = this.getClass().getClassLoader();
  82. java.net.URL imageURL = cldr.getResource("uk/org/ownage/dmdirc/res/icon.png");
  83. imageIcon = new ImageIcon(imageURL);
  84. setIconImage(imageIcon.getImage());
  85. setVisible(true);
  86. miAddServer.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent actionEvent) {
  88. NewServerDialog.showNewServerDialog();
  89. }
  90. });
  91. toggleStateMenuItem.addActionListener(new ActionListener() {
  92. public void actionPerformed(ActionEvent actionEvent) {
  93. try {
  94. getActiveFrame().setMaximum(!getActiveFrame().isMaximum());
  95. } catch (PropertyVetoException ex) {
  96. Logger.error(ErrorLevel.WARNING, ex);
  97. }
  98. }
  99. });
  100. addWindowListener(this);
  101. checkWindowState();
  102. }
  103. /**
  104. * Adds the specified InternalFrame as a child of the main frame
  105. * @param frame the frame to be added
  106. */
  107. public void addChild(JInternalFrame frame) {
  108. // Add the frame
  109. desktopPane.add(frame);
  110. // Make sure it'll fit with our offsets
  111. if (frame.getWidth()+xOffset > desktopPane.getWidth()) {
  112. xOffset = 0;
  113. }
  114. if (frame.getHeight()+yOffset > desktopPane.getHeight()) {
  115. yOffset = 0;
  116. }
  117. // Position the frame
  118. frame.setLocation(xOffset, yOffset);
  119. frame.moveToFront();
  120. // Increase the offsets
  121. xOffset += 30;
  122. yOffset += 30;
  123. }
  124. public void delChild(JInternalFrame frame) {
  125. desktopPane.remove(frame);
  126. }
  127. public ImageIcon getIcon() {
  128. return imageIcon;
  129. }
  130. /**
  131. * Returns the JInternalFrame that is currently active
  132. * @return The active JInternalFrame
  133. */
  134. public JInternalFrame getActiveFrame() {
  135. return desktopPane.getSelectedFrame();
  136. }
  137. /**
  138. * Sets whether or not the internal frame state is currently maximised
  139. * @param max whether the frame is maxomised
  140. */
  141. public void setMaximised(boolean max) {
  142. maximised = max;
  143. if (max) {
  144. if (getActiveFrame() != null) {
  145. setTitle("DMDirc - "+getActiveFrame().getTitle());
  146. }
  147. } else {
  148. setTitle("DMDirc");
  149. }
  150. for (JInternalFrame frame : desktopPane.getAllFrames()) {
  151. try {
  152. frame.setMaximum(max);
  153. } catch (PropertyVetoException ex) {
  154. Logger.error(ErrorLevel.WARNING, ex);
  155. }
  156. }
  157. checkWindowState();
  158. }
  159. /**
  160. * Gets whether or not the internal frame state is currently maximised
  161. * @return True iff frames should be maximised, false otherwise
  162. */
  163. public boolean getMaximised() {
  164. return maximised;
  165. }
  166. private void checkWindowState() {
  167. if (getActiveFrame() == null) {
  168. toggleStateMenuItem.setEnabled(false);
  169. return;
  170. }
  171. toggleStateMenuItem.setEnabled(true);
  172. if (maximised) {
  173. toggleStateMenuItem.setText("Restore");
  174. toggleStateMenuItem.setMnemonic('r');
  175. toggleStateMenuItem.invalidate();
  176. } else {
  177. toggleStateMenuItem.setText("Maximise");
  178. toggleStateMenuItem.setMnemonic('m');
  179. toggleStateMenuItem.invalidate();
  180. }
  181. }
  182. public void windowOpened(WindowEvent windowEvent) {
  183. }
  184. public void windowClosing(WindowEvent windowEvent) {
  185. ServerManager.getServerManager().closeAll(Config.getOption("general","closemessage"));
  186. Config.save();
  187. }
  188. public void windowClosed(WindowEvent windowEvent) {
  189. }
  190. public void windowIconified(WindowEvent windowEvent) {
  191. }
  192. public void windowDeiconified(WindowEvent windowEvent) {
  193. }
  194. public void windowActivated(WindowEvent windowEvent) {
  195. }
  196. public void windowDeactivated(WindowEvent windowEvent) {
  197. }
  198. /** This method is called from within the constructor to
  199. * initialize the form.
  200. * WARNING: Do NOT modify this code. The content of this method is
  201. * always regenerated by the Form Editor.
  202. */
  203. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  204. private void initComponents() {
  205. desktopPane = new javax.swing.JDesktopPane();
  206. jMenuBar1 = new javax.swing.JMenuBar();
  207. fileMenu = new javax.swing.JMenu();
  208. miAddServer = new javax.swing.JMenuItem();
  209. windowMenu = new javax.swing.JMenu();
  210. toggleStateMenuItem = new javax.swing.JMenuItem();
  211. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  212. setTitle("DMDirc");
  213. desktopPane.setBackground(new java.awt.Color(238, 238, 238));
  214. fileMenu.setMnemonic('f');
  215. fileMenu.setText("File");
  216. miAddServer.setText("New Server...");
  217. fileMenu.add(miAddServer);
  218. jMenuBar1.add(fileMenu);
  219. windowMenu.setMnemonic('w');
  220. windowMenu.setText("Window");
  221. toggleStateMenuItem.setMnemonic('m');
  222. toggleStateMenuItem.setText("Maximise");
  223. windowMenu.add(toggleStateMenuItem);
  224. jMenuBar1.add(windowMenu);
  225. setJMenuBar(jMenuBar1);
  226. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  227. getContentPane().setLayout(layout);
  228. layout.setHorizontalGroup(
  229. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  230. .add(org.jdesktop.layout.GroupLayout.TRAILING, desktopPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
  231. );
  232. layout.setVerticalGroup(
  233. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  234. .add(org.jdesktop.layout.GroupLayout.TRAILING, desktopPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
  235. );
  236. pack();
  237. }// </editor-fold>//GEN-END:initComponents
  238. // Variables declaration - do not modify//GEN-BEGIN:variables
  239. private javax.swing.JDesktopPane desktopPane;
  240. private javax.swing.JMenu fileMenu;
  241. private javax.swing.JMenuBar jMenuBar1;
  242. private javax.swing.JMenuItem miAddServer;
  243. private javax.swing.JMenuItem toggleStateMenuItem;
  244. private javax.swing.JMenu windowMenu;
  245. // End of variables declaration//GEN-END:variables
  246. }