/plugins/Snipplr/tags/0.1/src/snipplr/SnippetForm.java

# · Java · 323 lines · 179 code · 62 blank · 82 comment · 12 complexity · 4aa18182d7f38b7bc8c8b31603863358 MD5 · raw file

  1. /*
  2. SnippetForm.java
  3. :tabSize=4:indentSize=4:noTabs=true:
  4. :folding=explicit:collapseFolds=1:
  5. This file written by Ian Lewis (IanLewis@member.fsf.org)
  6. Copyright (C) 2007 Ian Lewis
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. Optionally, you may find a copy of the GNU General Public License
  19. from http://www.fsf.org/copyleft/gpl.txt
  20. */
  21. package snipplr;
  22. //{{{ Imports
  23. import org.gjt.sp.jedit.*;
  24. import org.gjt.sp.jedit.gui.EnhancedDialog;
  25. import org.gjt.sp.util.Log;
  26. import org.apache.xmlrpc.XmlRpcException;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import javax.swing.*;
  32. import javax.swing.border.EmptyBorder;
  33. import java.util.Vector;
  34. //}}}
  35. public class SnippetForm extends EnhancedDialog {
  36. //{{{ SnippetForm constructor
  37. public SnippetForm(Frame parent, Snippet snippet) {
  38. super(parent, jEdit.getProperty("snipplr-snippet.edit-snippet.label"), true);
  39. m_snippet = snippet;
  40. //setLayout(new BorderLayout());
  41. JPanel content = new JPanel(new BorderLayout(12,12));
  42. content.setBorder(new EmptyBorder(12,12,12,12));
  43. setContentPane(content);
  44. JPanel panel = new JPanel();
  45. GridBagLayout gridBag = new GridBagLayout();
  46. panel.setLayout(gridBag);
  47. int y = 0;
  48. //TODO: add a link at the top to allow you to open the browser to the
  49. //snippet
  50. // label = new JLabel(jEdit.getProperty("snipplr-snippet.url.label"));
  51. // cons = new GridBagConstraints();
  52. // cons.gridy = y++;
  53. // cons.gridheight = 1;
  54. // cons.gridwidth = 1;
  55. // cons.weightx = 0.0f;
  56. // cons.insets = new Insets(1,0,1,0);
  57. // cons.fill = GridBagConstraints.BOTH;
  58. // gridBag.setConstraints(label,cons);
  59. // panel.add(label);
  60. // m_url = new JTextField(snippet.getURL());
  61. // cons.fill = GridBagConstraints.HORIZONTAL;
  62. // cons.gridx = 1;
  63. // cons.weightx = 1.0f;
  64. // gridBag.setConstraints(m_url,cons);
  65. // panel.add(m_url);
  66. JLabel label = new JLabel(jEdit.getProperty("snipplr-snippet.title.label"));
  67. GridBagConstraints cons = new GridBagConstraints();
  68. cons.gridy = y++;
  69. cons.gridheight = 1;
  70. cons.gridwidth = 1;
  71. cons.weightx = 0.0f;
  72. cons.insets = new Insets(1,0,1,0);
  73. cons.fill = GridBagConstraints.BOTH;
  74. gridBag.setConstraints(label,cons);
  75. panel.add(label);
  76. m_title = new JTextField(snippet.getTitle());
  77. cons.fill = GridBagConstraints.HORIZONTAL;
  78. cons.gridx = 1;
  79. cons.weightx = 1.0f;
  80. gridBag.setConstraints(m_title,cons);
  81. panel.add(m_title);
  82. label = new JLabel(jEdit.getProperty("snipplr-snippet.tags.label"));
  83. cons = new GridBagConstraints();
  84. cons.gridy = y++;
  85. cons.gridheight = 1;
  86. cons.gridwidth = 1;
  87. cons.weightx = 0.0f;
  88. cons.insets = new Insets(1,0,1,0);
  89. cons.fill = GridBagConstraints.BOTH;
  90. gridBag.setConstraints(label,cons);
  91. panel.add(label);
  92. m_tags = new JTextField(snippet.getTagsString());
  93. cons.fill = GridBagConstraints.HORIZONTAL;
  94. cons.gridx = 1;
  95. cons.weightx = 1.0f;
  96. gridBag.setConstraints(m_tags,cons);
  97. panel.add(m_tags);
  98. label = new JLabel(jEdit.getProperty("snipplr-snippet.language.label"));
  99. cons = new GridBagConstraints();
  100. cons.gridy = y++;
  101. cons.gridheight = 1;
  102. cons.gridwidth = 1;
  103. cons.weightx = 0.0f;
  104. cons.insets = new Insets(1,0,1,0);
  105. cons.fill = GridBagConstraints.BOTH;
  106. gridBag.setConstraints(label,cons);
  107. panel.add(label);
  108. m_language = new JComboBox(new Vector<Language>(LanguageMapper.getLanguages().values()));
  109. if (snippet.getLanguage() == null) {
  110. m_language.setSelectedItem(LanguageMapper.languageSearch(jEdit.getActiveView().getBuffer().getMode().getName()).getHumanReadableName());
  111. } else {
  112. m_language.setSelectedItem(snippet.getLanguage());
  113. }
  114. cons.fill = GridBagConstraints.BOTH;
  115. cons.gridx = 1;
  116. cons.weightx = 1.0f;
  117. gridBag.setConstraints(m_language,cons);
  118. panel.add(m_language);
  119. // label = new JLabel(jEdit.getProperty("snipplr-snippet.comment.label"));
  120. // cons = new GridBagConstraints();
  121. // cons.gridy = y+=3;
  122. // cons.gridx = 0;
  123. // cons.gridheight = 3;
  124. // cons.gridwidth = 1;
  125. // cons.weightx = 0.0f;
  126. // cons.insets = new Insets(1,0,1,0);
  127. // cons.fill = GridBagConstraints.BOTH;
  128. // gridBag.setConstraints(label,cons);
  129. // panel.add(label);
  130. // m_comment = new JTextArea(snippet.getComment());
  131. // JScrollPane scrollPane = new JScrollPane(m_comment);
  132. // cons.fill = GridBagConstraints.BOTH;
  133. // cons.gridx = 1;
  134. // cons.weightx = 1.0f;
  135. // cons.weighty = 1.0f;
  136. // gridBag.setConstraints(scrollPane,cons);
  137. // panel.add(scrollPane);
  138. label = new JLabel(jEdit.getProperty("snipplr-snippet.source.label"));
  139. cons = new GridBagConstraints();
  140. cons.gridy = y+=3;
  141. cons.gridx = 0;
  142. cons.gridheight = 3;
  143. cons.gridwidth = 1;
  144. cons.weightx = 0.0f;
  145. cons.insets = new Insets(1,0,1,0);
  146. cons.fill = GridBagConstraints.BOTH;
  147. gridBag.setConstraints(label,cons);
  148. panel.add(label);
  149. m_source = new JTextArea(snippet.getSource());
  150. JScrollPane scrollPane = new JScrollPane(m_source);
  151. cons.fill = GridBagConstraints.BOTH;
  152. cons.gridx = 1;
  153. cons.weightx = 1.0f;
  154. cons.weighty = 2.0f;
  155. gridBag.setConstraints(scrollPane,cons);
  156. panel.add(scrollPane);
  157. getContentPane().add(BorderLayout.CENTER, panel);
  158. Box buttons = new Box(BoxLayout.X_AXIS);
  159. buttons.add(Box.createGlue());
  160. //If the snippet is new then we can enable the save button.
  161. if (snippet.getId() == null) {
  162. m_okButton = new JButton(jEdit.getProperty("snipplr.save"));
  163. m_okButton.addActionListener(new ActionListener() {
  164. public void actionPerformed(ActionEvent evt) {
  165. ok();
  166. }
  167. });
  168. buttons.add(m_okButton);
  169. buttons.add(Box.createHorizontalStrut(6));
  170. }
  171. m_cancelButton = new JButton(jEdit.getProperty("common.cancel"));
  172. m_cancelButton.addActionListener(new ActionListener() {
  173. public void actionPerformed(ActionEvent evt) {
  174. cancel();
  175. }
  176. });
  177. buttons.add(m_cancelButton);
  178. buttons.add(Box.createGlue());
  179. getContentPane().add(buttons, BorderLayout.SOUTH);
  180. //we have multi-line text areas so don't handle Enter.
  181. setEnterEnabled(false);
  182. //The snipplr service does not provide a mechanism to update snippets.
  183. if (snippet.getId() != null) {
  184. m_title.setEnabled(false);
  185. m_tags.setEnabled(false);
  186. m_language.setEnabled(false);
  187. // m_comment.setEnabled(false);
  188. m_source.setEnabled(false);
  189. }
  190. GUIUtilities.loadGeometry(this, parent, m_name);
  191. // updateSize();
  192. setVisible(true);
  193. }//}}}
  194. //{{{ ok()
  195. public void ok() {
  196. //TODO: Save the Snippet to the server.
  197. //Only save the snippet if it's new.
  198. //JOptionPane.showMessageDialog(this, "Save!");
  199. if (m_snippet.getId() == null) {
  200. try {
  201. //Save the snippet and post it.
  202. m_snippet.setTitle(m_title.getText());
  203. m_snippet.setTagsString(m_tags.getText());
  204. m_snippet.setLanguage((Language)m_language.getSelectedItem());
  205. m_snippet.setSource(m_source.getText());
  206. SnipplrService.snippetPost(m_snippet);
  207. dispose();
  208. } catch (XmlRpcException e) {
  209. Log.log(Log.ERROR, SnipplrService.class, e);
  210. GUIUtilities.error(this,"snipplr.request.error", new String[] { e.getMessage() });
  211. }
  212. }
  213. }//}}}
  214. //{{{ cancel()
  215. public void cancel() {
  216. dispose();
  217. }//}}}
  218. //{{{ dispose()
  219. public void dispose() {
  220. GUIUtilities.saveGeometry(this, m_name);
  221. super.dispose();
  222. }//}}}
  223. //{{{ Private members
  224. //{{{ updateSize() method
  225. private void updateSize() {
  226. Dimension currentSize = getSize();
  227. Dimension requestedSize = getPreferredSize();
  228. Dimension newSize = new Dimension(
  229. Math.max(currentSize.width,requestedSize.width),
  230. Math.max(currentSize.height,requestedSize.height)
  231. );
  232. boolean update = false;
  233. if (newSize.width < 300) {
  234. newSize.width = 300;
  235. update = true;
  236. }
  237. if (newSize.height < 200) {
  238. newSize.height = 200;
  239. update = true;
  240. }
  241. if (update) {
  242. setSize(newSize);
  243. }
  244. validate();
  245. } //}}}
  246. private static final String m_name = "edit.snippet";
  247. private JButton m_okButton;
  248. private JButton m_cancelButton;
  249. private Snippet m_snippet;
  250. private JTextField m_title;
  251. private JTextField m_tags;
  252. // private JTextField m_url;
  253. private JComboBox m_language;
  254. // private JTextArea m_comment;
  255. private JTextArea m_source;
  256. //}}}
  257. }