You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainFrame.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Singleton instance of MainFrame
  43. */
  44. private static MainFrame me;
  45. /**
  46. * Whether the internal frames are maximised or not
  47. */
  48. private boolean maximised;
  49. private int xOffset = 0;
  50. private int yOffset = 0;
  51. /**
  52. * Returns the singleton instance of MainFrame
  53. * @return MainFrame instance
  54. */
  55. public static MainFrame getMainFrame() {
  56. if (me == null) {
  57. me = new MainFrame();
  58. }
  59. return me;
  60. }
  61. /** Creates new form MainFrame */
  62. public MainFrame() {
  63. initComponents();
  64. // Load an icon
  65. ClassLoader cldr = this.getClass().getClassLoader();
  66. java.net.URL imageURL = cldr.getResource("uk/org/ownage/dmdirc/res/icon.png");
  67. setIconImage(new ImageIcon(imageURL).getImage());
  68. setVisible(true);
  69. miAddServer.addActionListener(new ActionListener() {
  70. public void actionPerformed(ActionEvent actionEvent) {
  71. NewServerDialog.showNewServerDialog();
  72. }
  73. });
  74. toggleStateMenuItem.addActionListener(new ActionListener() {
  75. public void actionPerformed(ActionEvent actionEvent) {
  76. try {
  77. getActiveFrame().setMaximum(!getActiveFrame().isMaximum());
  78. } catch (PropertyVetoException ex) {
  79. Logger.error(ErrorLevel.WARNING, ex);
  80. }
  81. }
  82. });
  83. addWindowListener(this);
  84. checkWindowState();
  85. }
  86. /**
  87. * Adds the specified InternalFrame as a child of the main frame
  88. * @param frame the frame to be added
  89. */
  90. public void addChild(JInternalFrame frame) {
  91. // Add the frame
  92. desktopPane.add(frame);
  93. // Make sure it'll fit with our offsets
  94. if (frame.getWidth()+xOffset > desktopPane.getWidth()) {
  95. xOffset = 0;
  96. }
  97. if (frame.getHeight()+yOffset > desktopPane.getHeight()) {
  98. yOffset = 0;
  99. }
  100. // Position the frame
  101. frame.setLocation(xOffset, yOffset);
  102. frame.moveToFront();
  103. // Increase the offsets
  104. xOffset += 30;
  105. yOffset += 30;
  106. }
  107. public void delChild(JInternalFrame frame) {
  108. desktopPane.remove(frame);
  109. }
  110. /**
  111. * Returns the JInternalFrame that is currently active
  112. * @return The active JInternalFrame
  113. */
  114. public JInternalFrame getActiveFrame() {
  115. return desktopPane.getSelectedFrame();
  116. }
  117. /**
  118. * Sets whether or not the internal frame state is currently maximised
  119. * @param max whether the frame is maxomised
  120. */
  121. public void setMaximised(boolean max) {
  122. maximised = max;
  123. if (max && getActiveFrame() != null) {
  124. setTitle("DMDirc 0.1 - "+getActiveFrame().getTitle());
  125. } else if (!max) {
  126. setTitle("DMDirc 0.1");
  127. }
  128. checkWindowState();
  129. }
  130. private void checkWindowState() {
  131. if (getActiveFrame() == null) {
  132. toggleStateMenuItem.setEnabled(false);
  133. return;
  134. }
  135. toggleStateMenuItem.setEnabled(true);
  136. if (maximised) {
  137. toggleStateMenuItem.setText("Restore");
  138. toggleStateMenuItem.setMnemonic('r');
  139. toggleStateMenuItem.invalidate();
  140. } else {
  141. toggleStateMenuItem.setText("Maximise");
  142. toggleStateMenuItem.setMnemonic('m');
  143. toggleStateMenuItem.invalidate();
  144. }
  145. }
  146. public void windowOpened(WindowEvent windowEvent) {
  147. }
  148. public void windowClosing(WindowEvent windowEvent) {
  149. ServerManager.getServerManager().closeAll(Config.getOption("general","closemessage"));
  150. Config.save();
  151. }
  152. public void windowClosed(WindowEvent windowEvent) {
  153. }
  154. public void windowIconified(WindowEvent windowEvent) {
  155. }
  156. public void windowDeiconified(WindowEvent windowEvent) {
  157. }
  158. public void windowActivated(WindowEvent windowEvent) {
  159. }
  160. public void windowDeactivated(WindowEvent windowEvent) {
  161. }
  162. /** This method is called from within the constructor to
  163. * initialize the form.
  164. * WARNING: Do NOT modify this code. The content of this method is
  165. * always regenerated by the Form Editor.
  166. */
  167. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  168. private void initComponents() {
  169. desktopPane = new javax.swing.JDesktopPane();
  170. jMenuBar1 = new javax.swing.JMenuBar();
  171. fileMenu = new javax.swing.JMenu();
  172. miAddServer = new javax.swing.JMenuItem();
  173. windowMenu = new javax.swing.JMenu();
  174. toggleStateMenuItem = new javax.swing.JMenuItem();
  175. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  176. setTitle("DMDirc 0.1");
  177. desktopPane.setBackground(new java.awt.Color(238, 238, 238));
  178. fileMenu.setMnemonic('f');
  179. fileMenu.setText("File");
  180. miAddServer.setText("New Server...");
  181. fileMenu.add(miAddServer);
  182. jMenuBar1.add(fileMenu);
  183. windowMenu.setMnemonic('w');
  184. windowMenu.setText("Window");
  185. toggleStateMenuItem.setMnemonic('m');
  186. toggleStateMenuItem.setText("Maximise");
  187. windowMenu.add(toggleStateMenuItem);
  188. jMenuBar1.add(windowMenu);
  189. setJMenuBar(jMenuBar1);
  190. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  191. getContentPane().setLayout(layout);
  192. layout.setHorizontalGroup(
  193. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  194. .add(org.jdesktop.layout.GroupLayout.TRAILING, desktopPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
  195. );
  196. layout.setVerticalGroup(
  197. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  198. .add(org.jdesktop.layout.GroupLayout.TRAILING, desktopPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
  199. );
  200. pack();
  201. }// </editor-fold>//GEN-END:initComponents
  202. // Variables declaration - do not modify//GEN-BEGIN:variables
  203. private javax.swing.JDesktopPane desktopPane;
  204. private javax.swing.JMenu fileMenu;
  205. private javax.swing.JMenuBar jMenuBar1;
  206. private javax.swing.JMenuItem miAddServer;
  207. private javax.swing.JMenuItem toggleStateMenuItem;
  208. private javax.swing.JMenu windowMenu;
  209. // End of variables declaration//GEN-END:variables
  210. }