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.

Frame.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 com.dmdirc.ui.components;
  23. import com.dmdirc.BrowserLauncher;
  24. import com.dmdirc.Config;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.identities.ConfigManager;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import com.dmdirc.ui.interfaces.InputWindow;
  30. import com.dmdirc.ui.MainFrame;
  31. import com.dmdirc.ui.messages.Formatter;
  32. import com.dmdirc.ui.messages.Styliser;
  33. import com.dmdirc.ui.textpane.IRCTextAttribute;
  34. import com.dmdirc.ui.textpane.TextPane;
  35. import com.dmdirc.ui.textpane.TextPaneListener;
  36. import java.awt.Color;
  37. import java.awt.Dimension;
  38. import java.awt.Point;
  39. import java.awt.Toolkit;
  40. import java.awt.datatransfer.StringSelection;
  41. import java.awt.event.ActionEvent;
  42. import java.awt.event.ActionListener;
  43. import java.awt.event.KeyEvent;
  44. import java.awt.event.KeyListener;
  45. import java.awt.event.MouseEvent;
  46. import java.awt.event.MouseListener;
  47. import java.beans.PropertyChangeEvent;
  48. import java.beans.PropertyChangeListener;
  49. import java.beans.PropertyVetoException;
  50. import java.lang.reflect.Constructor;
  51. import java.lang.reflect.InvocationTargetException;
  52. import java.text.AttributedCharacterIterator;
  53. import java.util.Date;
  54. import javax.swing.BorderFactory;
  55. import javax.swing.JInternalFrame;
  56. import javax.swing.JMenuItem;
  57. import javax.swing.JPopupMenu;
  58. import javax.swing.SwingUtilities;
  59. import javax.swing.UIManager;
  60. import javax.swing.event.InternalFrameEvent;
  61. import javax.swing.event.InternalFrameListener;
  62. import javax.swing.plaf.basic.BasicInternalFrameUI;
  63. import javax.swing.plaf.synth.SynthLookAndFeel;
  64. /**
  65. * Implements a generic (internal) frame.
  66. */
  67. public abstract class Frame extends JInternalFrame implements InputWindow,
  68. PropertyChangeListener, InternalFrameListener,
  69. MouseListener, ActionListener, KeyListener, TextPaneListener {
  70. /**
  71. * A version number for this class. It should be changed whenever the class
  72. * structure is changed (or anything else that would prevent serialized
  73. * objects being unserialized with the new class).
  74. */
  75. private static final long serialVersionUID = 5;
  76. /** Frame output pane. */
  77. private TextPane textPane;
  78. /** The channel object that owns this frame. */
  79. private final FrameContainer parent;
  80. /** Popupmenu for this frame. */
  81. private JPopupMenu popup;
  82. /** popup menu item. */
  83. private JMenuItem copyMI;
  84. /** hyperlink menu item. */
  85. private JMenuItem hyperlinkCopyMI;
  86. /** hyperlink menu item. */
  87. private JMenuItem hyperlinkOpenMI;
  88. /** search bar. */
  89. private SearchBar searchBar;
  90. /**
  91. * Creates a new instance of Frame.
  92. *
  93. * @param owner FrameContainer owning this frame.
  94. */
  95. public Frame(final FrameContainer owner) {
  96. super();
  97. parent = owner;
  98. setFrameIcon(MainFrame.getMainFrame().getIcon());
  99. initComponents();
  100. setMaximizable(true);
  101. setClosable(true);
  102. setResizable(true);
  103. setIconifiable(true);
  104. setPreferredSize(new Dimension(MainFrame.getMainFrame().getWidth() / 2,
  105. MainFrame.getMainFrame().getHeight() / 3));
  106. addPropertyChangeListener("maximum", this);
  107. addInternalFrameListener(this);
  108. final ConfigManager config = owner.getConfigManager();
  109. getTextPane().setBackground(config.getOptionColour("ui", "backgroundcolour", Color.WHITE));
  110. getTextPane().setForeground(config.getOptionColour("ui", "foregroundcolour", Color.BLACK));
  111. final Boolean pref = Config.getOptionBool("ui", "maximisewindows");
  112. if (pref || MainFrame.getMainFrame().getMaximised()) {
  113. hideTitlebar();
  114. }
  115. }
  116. /**
  117. * Makes this frame visible. We don't call this from the constructor
  118. * so that we can register an actionlistener for the open event before
  119. * the frame is opened.
  120. */
  121. public void open() {
  122. setVisible(true);
  123. }
  124. /**
  125. * Adds a line of text to the main text area.
  126. *
  127. * @param line text to add
  128. * @param timestamp Whether to timestamp the line or not
  129. */
  130. public final void addLine(final String line, final boolean timestamp) {
  131. SwingUtilities.invokeLater(new Runnable() {
  132. public void run() {
  133. for (String myLine : line.split("\n")) {
  134. if (timestamp) {
  135. Styliser.addStyledString(getTextPane(), new String[]{
  136. Formatter.formatMessage("timestamp", new Date()),
  137. myLine, });
  138. } else {
  139. Styliser.addStyledString(getTextPane(), myLine);
  140. }
  141. textPane.trim(Config.getOptionInt("ui", "frameBufferSize", Integer.MAX_VALUE));
  142. }
  143. }
  144. });
  145. }
  146. /** {@inheritDoc} */
  147. public final void addLine(final String messageType, final Object... args) {
  148. if (messageType.length() > 0) {
  149. addLine(Formatter.formatMessage(messageType, args), true);
  150. }
  151. }
  152. /** {@inheritDoc} */
  153. public final void addLine(final StringBuffer messageType, final Object... args) {
  154. if (messageType != null) {
  155. addLine(messageType.toString(), args);
  156. }
  157. }
  158. /** {@inheritDoc} */
  159. public final void clear() {
  160. getTextPane().clear();
  161. }
  162. /**
  163. * Initialises the components for this frame.
  164. */
  165. private void initComponents() {
  166. setTextPane(new TextPane(this));
  167. getTextPane().addMouseListener(this);
  168. getTextPane().addKeyListener(this);
  169. getTextPane().addTextPaneListener(this);
  170. popup = new JPopupMenu();
  171. copyMI = new JMenuItem("Copy");
  172. copyMI.addActionListener(this);
  173. hyperlinkCopyMI = new JMenuItem("Copy URL");
  174. hyperlinkCopyMI.addActionListener(this);
  175. hyperlinkCopyMI.setVisible(false);
  176. hyperlinkOpenMI = new JMenuItem("Open URL");
  177. hyperlinkOpenMI.addActionListener(this);
  178. hyperlinkOpenMI.setVisible(false);
  179. popup.add(hyperlinkOpenMI);
  180. popup.add(hyperlinkCopyMI);
  181. popup.add(copyMI);
  182. popup.setOpaque(true);
  183. popup.setLightWeightPopupEnabled(true);
  184. searchBar = new SearchBar(this);
  185. searchBar.setVisible(false);
  186. }
  187. /**
  188. * Removes and reinserts the border of an internal frame on maximising.
  189. * {@inheritDoc}
  190. */
  191. public final void propertyChange(final PropertyChangeEvent event) {
  192. if (event.getNewValue().equals(Boolean.TRUE)) {
  193. hideTitlebar();
  194. MainFrame.getMainFrame().setMaximised(true);
  195. } else {
  196. showTitlebar();
  197. MainFrame.getMainFrame().setMaximised(false);
  198. MainFrame.getMainFrame().setActiveFrame(this);
  199. }
  200. }
  201. /** Hides the titlebar for this frame. */
  202. private void hideTitlebar() {
  203. setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  204. ((BasicInternalFrameUI) getUI()).setNorthPane(null);
  205. }
  206. /** Shows the titlebar for this frame. */
  207. private void showTitlebar() {
  208. final Class< ? > c;
  209. Object temp = null;
  210. Constructor< ? > constructor;
  211. final String componentUI = (String) UIManager.get("InternalFrameUI");
  212. if ("javax.swing.plaf.synth.SynthLookAndFeel".equals(componentUI)) {
  213. temp = SynthLookAndFeel.createUI(this);
  214. } else {
  215. try {
  216. c = getClass().getClassLoader().loadClass(componentUI);
  217. constructor = c.getConstructor(new Class[] {javax.swing.JInternalFrame.class});
  218. temp = constructor.newInstance(new Object[] {this});
  219. } catch (ClassNotFoundException ex) {
  220. Logger.error(ErrorLevel.WARNING, "Unable to readd titlebar", ex);
  221. } catch (NoSuchMethodException ex) {
  222. Logger.error(ErrorLevel.WARNING, "Unable to readd titlebar", ex);
  223. } catch (InstantiationException ex) {
  224. Logger.error(ErrorLevel.WARNING, "Unable to readd titlebar", ex);
  225. } catch (IllegalAccessException ex) {
  226. Logger.error(ErrorLevel.WARNING, "Unable to readd titlebar", ex);
  227. } catch (InvocationTargetException ex) {
  228. Logger.error(ErrorLevel.WARNING, "Unable to readd titlebar", ex);
  229. }
  230. }
  231. setBorder(UIManager.getBorder("InternalFrame.border"));
  232. if (temp == null) {
  233. temp = new BasicInternalFrameUI(this);
  234. }
  235. this.setUI((BasicInternalFrameUI) temp);
  236. }
  237. /**
  238. * Not needed for this class. {@inheritDoc}
  239. */
  240. public void internalFrameOpened(final InternalFrameEvent event) {
  241. parent.windowOpened();
  242. }
  243. /**
  244. * Not needed for this class. {@inheritDoc}
  245. */
  246. public void internalFrameClosing(final InternalFrameEvent event) {
  247. parent.windowClosing();
  248. }
  249. /**
  250. * Not needed for this class. {@inheritDoc}
  251. */
  252. public void internalFrameClosed(final InternalFrameEvent event) {
  253. parent.windowClosed();
  254. }
  255. /**
  256. * Makes the internal frame invisible. {@inheritDoc}
  257. */
  258. public void internalFrameIconified(final InternalFrameEvent event) {
  259. event.getInternalFrame().setVisible(false);
  260. }
  261. /**
  262. * Not needed for this class. {@inheritDoc}
  263. */
  264. public void internalFrameDeiconified(final InternalFrameEvent event) {
  265. //Ignore.
  266. }
  267. /**
  268. * Activates the input field on frame focus. {@inheritDoc}
  269. */
  270. public void internalFrameActivated(final InternalFrameEvent event) {
  271. parent.windowActivated();
  272. }
  273. /**
  274. * Not needed for this class. {@inheritDoc}
  275. */
  276. public void internalFrameDeactivated(final InternalFrameEvent event) {
  277. parent.windowDeactivated();
  278. }
  279. /** {@inheritDoc} */
  280. public FrameContainer getContainer() {
  281. return parent;
  282. }
  283. /** {@inheritDoc} */
  284. public ConfigManager getConfigManager() {
  285. return getContainer().getConfigManager();
  286. }
  287. /**
  288. * Returns the text pane for this frame.
  289. *
  290. * @return Text pane for this frame
  291. */
  292. public final TextPane getTextPane() {
  293. return textPane;
  294. }
  295. /** {@inheritDoc} */
  296. @Override
  297. public final String getName() {
  298. return parent.toString();
  299. }
  300. /**
  301. * Sets the frames text pane.
  302. *
  303. * @param newTextPane new text pane to use
  304. */
  305. protected final void setTextPane(final TextPane newTextPane) {
  306. this.textPane = newTextPane;
  307. }
  308. /**
  309. * Checks for url's, channels and nicknames. {@inheritDoc}
  310. */
  311. public void mouseClicked(final MouseEvent mouseEvent) {
  312. if (mouseEvent.getSource() == getTextPane()) {
  313. processMouseEvent(mouseEvent);
  314. }
  315. }
  316. /**
  317. * Not needed for this class. {@inheritDoc}
  318. */
  319. public void mousePressed(final MouseEvent mouseEvent) {
  320. processMouseEvent(mouseEvent);
  321. }
  322. /**
  323. * Not needed for this class. {@inheritDoc}
  324. */
  325. public void mouseReleased(final MouseEvent mouseEvent) {
  326. if (Config.getOptionBool("ui", "quickCopy") && mouseEvent.getSource() == getTextPane()) {
  327. getTextPane().copy();
  328. getTextPane().clearSelection();
  329. }
  330. processMouseEvent(mouseEvent);
  331. }
  332. /**
  333. * Not needed for this class. {@inheritDoc}
  334. */
  335. public void mouseEntered(final MouseEvent mouseEvent) {
  336. //Ignore.
  337. }
  338. /**
  339. * Not needed for this class. {@inheritDoc}
  340. */
  341. public void mouseExited(final MouseEvent mouseEvent) {
  342. //Ignore.
  343. }
  344. /**
  345. * Processes every mouse button event to check for a popup trigger.
  346. *
  347. * @param e mouse event
  348. */
  349. @Override
  350. public void processMouseEvent(final MouseEvent e) {
  351. if (e.isPopupTrigger() && e.getSource() == getTextPane()) {
  352. final Point point = getTextPane().getMousePosition();
  353. if (point != null) {
  354. hyperlinkOpenMI.setActionCommand("");
  355. hyperlinkOpenMI.setVisible(false);
  356. hyperlinkCopyMI.setVisible(false);
  357. final int[] info = textPane.getClickPosition(point);
  358. if (info[0] != -1) {
  359. final AttributedCharacterIterator iterator = textPane.getLine(info[0]).getIterator();
  360. iterator.setIndex(info[2]);
  361. final Object linkattr = iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
  362. if (linkattr instanceof String) {
  363. hyperlinkCopyMI.setVisible(true);
  364. hyperlinkOpenMI.setVisible(true);
  365. hyperlinkOpenMI.setActionCommand((String) linkattr);
  366. }
  367. }
  368. final int[] selection = textPane.getSelectedRange();
  369. if ((selection[0] == selection[2] && selection[1] == selection[3])) {
  370. copyMI.setEnabled(false);
  371. } else {
  372. copyMI.setEnabled(true);
  373. }
  374. getPopup().show(this, (int) point.getX(), (int) point.getY());
  375. }
  376. }
  377. super.processMouseEvent(e);
  378. }
  379. /** {@inheritDoc} */
  380. public void actionPerformed(final ActionEvent actionEvent) {
  381. if (actionEvent.getSource() == copyMI) {
  382. getTextPane().copy();
  383. } else if (actionEvent.getSource() == hyperlinkCopyMI) {
  384. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
  385. new StringSelection(hyperlinkOpenMI.getActionCommand()), null);
  386. } else if (actionEvent.getSource() == hyperlinkOpenMI) {
  387. BrowserLauncher.openURL(hyperlinkOpenMI.getActionCommand());
  388. }
  389. }
  390. /**
  391. * returns the popup menu for this frame.
  392. *
  393. * @return JPopupMenu for this frame
  394. */
  395. public final JPopupMenu getPopup() {
  396. return popup;
  397. }
  398. /** {@inheritDoc} */
  399. public void keyTyped(final KeyEvent event) {
  400. //Ignore.
  401. }
  402. /** {@inheritDoc} */
  403. public void keyPressed(final KeyEvent event) {
  404. if ((event.getModifiers() & KeyEvent.CTRL_MASK) == 0) {
  405. if (event.getKeyCode() == KeyEvent.VK_PAGE_UP) {
  406. getTextPane().pageUp();
  407. } else if (event.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
  408. getTextPane().pageDown();
  409. }
  410. } else {
  411. if (event.getKeyCode() == KeyEvent.VK_HOME) {
  412. getTextPane().setScrollBarPosition(0);
  413. } else if (event.getKeyCode() == KeyEvent.VK_END) {
  414. getTextPane().setScrollBarPosition(textPane.getNumLines());
  415. }
  416. }
  417. if (event.getKeyCode() == KeyEvent.VK_F3) {
  418. if (!getSearchBar().isVisible()) {
  419. getSearchBar().open();
  420. }
  421. getSearchBar().search();
  422. }
  423. if (event.getKeyCode() == KeyEvent.VK_F
  424. && (event.getModifiers() & KeyEvent.CTRL_MASK) != 0
  425. && (event.getModifiers() & KeyEvent.SHIFT_MASK) == 0) {
  426. doSearchBar();
  427. }
  428. if (!Config.getOptionBool("ui", "quickCopy")
  429. && (event.getModifiers() & KeyEvent.CTRL_MASK) != 0
  430. && event.getKeyCode() == KeyEvent.VK_C) {
  431. getTextPane().copy();
  432. }
  433. }
  434. /** Opens, closes or focuses the search bar as appropriate. */
  435. private void doSearchBar() {
  436. if (getSearchBar().isVisible()) {
  437. getSearchBar().getFocus();
  438. } else {
  439. getSearchBar().open();
  440. }
  441. }
  442. /** {@inheritDoc} */
  443. public void keyReleased(final KeyEvent event) {
  444. //Ignore.
  445. }
  446. /** {@inheritDoc} */
  447. public void hyperlinkClicked(final String url) {
  448. MainFrame.getMainFrame().getStatusBar().setMessage("Opening: " + url);
  449. BrowserLauncher.openURL(url);
  450. }
  451. /** {@inheritDoc} */
  452. public void channelClicked(final String channel) {
  453. if (parent.getServer().getParser().getChannelInfo(channel) == null) {
  454. parent.getServer().getParser().joinChannel(channel);
  455. } else {
  456. parent.getServer().getChannel(channel).activateFrame();
  457. }
  458. }
  459. /**
  460. * Gets the search bar.
  461. *
  462. * @return the frames search bar
  463. */
  464. public final SearchBar getSearchBar() {
  465. return searchBar;
  466. }
  467. /** Closes this frame. */
  468. public void close() {
  469. try {
  470. setClosed(true);
  471. } catch (PropertyVetoException ex) {
  472. Logger.error(ErrorLevel.WARNING, "Unable to close frame", ex);
  473. }
  474. }
  475. /** Minimises the frame. */
  476. public void minimise() {
  477. try {
  478. setIcon(true);
  479. } catch (PropertyVetoException ex) {
  480. Logger.error(ErrorLevel.WARNING, "Unable to minimise frame", ex);
  481. }
  482. }
  483. }