|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * Arduino is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 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 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +package cc.arduino.view.preferences; |
| 31 | + |
| 32 | +import com.google.common.base.Function; |
| 33 | +import com.google.common.base.Joiner; |
| 34 | +import com.google.common.base.Predicate; |
| 35 | +import com.google.common.collect.FluentIterable; |
| 36 | +import processing.app.Base; |
| 37 | + |
| 38 | +import java.awt.event.ActionEvent; |
| 39 | +import java.awt.event.ActionListener; |
| 40 | +import java.awt.event.WindowEvent; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Collection; |
| 43 | + |
| 44 | +import static processing.app.I18n._; |
| 45 | + |
| 46 | +public class AdditionalBoardsManagerURLTextArea extends javax.swing.JDialog { |
| 47 | + |
| 48 | + private ActionListener onOkListener; |
| 49 | + |
| 50 | + public AdditionalBoardsManagerURLTextArea(java.awt.Frame parent) { |
| 51 | + super(parent); |
| 52 | + initComponents(); |
| 53 | + setLocationRelativeTo(parent); |
| 54 | + |
| 55 | + Base.registerWindowCloseKeys(getRootPane(), new ActionListener() { |
| 56 | + @Override |
| 57 | + public void actionPerformed(ActionEvent e) { |
| 58 | + cancelActionPerformed(e); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * This method is called from within the constructor to initialize the form. |
| 66 | + * WARNING: Do NOT modify this code. The content of this method is always |
| 67 | + * regenerated by the Form Editor. |
| 68 | + */ |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
| 71 | + private void initComponents() { |
| 72 | + |
| 73 | + javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane(); |
| 74 | + javax.swing.JButton cancel = new javax.swing.JButton(); |
| 75 | + javax.swing.JButton ok = new javax.swing.JButton(); |
| 76 | + |
| 77 | + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); |
| 78 | + setTitle(_("Additional Boards Manager URLs")); |
| 79 | + setModal(true); |
| 80 | + setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE); |
| 81 | + |
| 82 | + additionalBoardsManagerURLs.setColumns(20); |
| 83 | + additionalBoardsManagerURLs.setRows(5); |
| 84 | + additionalBoardsManagerURLs.setName(""); // NOI18N |
| 85 | + jScrollPane1.setViewportView(additionalBoardsManagerURLs); |
| 86 | + |
| 87 | + cancel.setText(_("Cancel")); |
| 88 | + cancel.addActionListener(new java.awt.event.ActionListener() { |
| 89 | + public void actionPerformed(java.awt.event.ActionEvent evt) { |
| 90 | + cancelActionPerformed(evt); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + ok.setText(_("OK")); |
| 95 | + ok.addActionListener(new java.awt.event.ActionListener() { |
| 96 | + public void actionPerformed(java.awt.event.ActionEvent evt) { |
| 97 | + okActionPerformed(evt); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); |
| 102 | + getContentPane().setLayout(layout); |
| 103 | + layout.setHorizontalGroup( |
| 104 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 105 | + .addGroup(layout.createSequentialGroup() |
| 106 | + .addContainerGap() |
| 107 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 108 | + .addGroup(layout.createSequentialGroup() |
| 109 | + .addGap(0, 439, Short.MAX_VALUE) |
| 110 | + .addComponent(ok) |
| 111 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 112 | + .addComponent(cancel)) |
| 113 | + .addComponent(jScrollPane1)) |
| 114 | + .addContainerGap()) |
| 115 | + ); |
| 116 | + layout.setVerticalGroup( |
| 117 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 118 | + .addGroup(layout.createSequentialGroup() |
| 119 | + .addContainerGap() |
| 120 | + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE) |
| 121 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 122 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 123 | + .addComponent(cancel) |
| 124 | + .addComponent(ok)) |
| 125 | + .addContainerGap()) |
| 126 | + ); |
| 127 | + |
| 128 | + pack(); |
| 129 | + }// </editor-fold>//GEN-END:initComponents |
| 130 | + |
| 131 | + private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed |
| 132 | + dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); |
| 133 | + }//GEN-LAST:event_cancelActionPerformed |
| 134 | + |
| 135 | + private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed |
| 136 | + ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""); |
| 137 | + onOkListener.actionPerformed(actionEvent); |
| 138 | + cancelActionPerformed(evt); |
| 139 | + }//GEN-LAST:event_okActionPerformed |
| 140 | + |
| 141 | + /** |
| 142 | + * @param args the command line arguments |
| 143 | + */ |
| 144 | + public static void main(String args[]) { |
| 145 | + /* Set the Nimbus look and feel */ |
| 146 | + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> |
| 147 | + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. |
| 148 | + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html |
| 149 | + */ |
| 150 | + try { |
| 151 | + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { |
| 152 | + if ("Nimbus".equals(info.getName())) { |
| 153 | + javax.swing.UIManager.setLookAndFeel(info.getClassName()); |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (ClassNotFoundException ex) { |
| 158 | + java.util.logging.Logger.getLogger(AdditionalBoardsManagerURLTextArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 159 | + } catch (InstantiationException ex) { |
| 160 | + java.util.logging.Logger.getLogger(AdditionalBoardsManagerURLTextArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 161 | + } catch (IllegalAccessException ex) { |
| 162 | + java.util.logging.Logger.getLogger(AdditionalBoardsManagerURLTextArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 163 | + } catch (javax.swing.UnsupportedLookAndFeelException ex) { |
| 164 | + java.util.logging.Logger.getLogger(AdditionalBoardsManagerURLTextArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 165 | + } |
| 166 | + //</editor-fold> |
| 167 | + |
| 168 | + /* Create and display the dialog */ |
| 169 | + java.awt.EventQueue.invokeLater(new Runnable() { |
| 170 | + public void run() { |
| 171 | + AdditionalBoardsManagerURLTextArea dialog = new AdditionalBoardsManagerURLTextArea(new javax.swing.JFrame()); |
| 172 | + dialog.addWindowListener(new java.awt.event.WindowAdapter() { |
| 173 | + @Override |
| 174 | + public void windowClosing(java.awt.event.WindowEvent e) { |
| 175 | + System.exit(0); |
| 176 | + } |
| 177 | + }); |
| 178 | + dialog.setVisible(true); |
| 179 | + } |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + public void setText(String text) { |
| 184 | + Collection<String> urls = splitAndTrim(text, ","); |
| 185 | + additionalBoardsManagerURLs.setText(Joiner.on("\n").skipNulls().join(urls)); |
| 186 | + } |
| 187 | + |
| 188 | + private Collection<String> splitAndTrim(String text, String separator) { |
| 189 | + Collection<String> urls = Arrays.asList(text.split(separator)); |
| 190 | + return FluentIterable.from(urls).transform(new Function<String, String>() { |
| 191 | + @Override |
| 192 | + public String apply(String input) { |
| 193 | + return input.trim(); |
| 194 | + } |
| 195 | + }).filter(new Predicate<String>() { |
| 196 | + @Override |
| 197 | + public boolean apply(String input) { |
| 198 | + return !input.isEmpty(); |
| 199 | + } |
| 200 | + }).toList(); |
| 201 | + } |
| 202 | + |
| 203 | + public String getText() { |
| 204 | + Collection<String> urls = splitAndTrim(additionalBoardsManagerURLs.getText(), "\n"); |
| 205 | + return Joiner.on(",").skipNulls().join(urls); |
| 206 | + } |
| 207 | + |
| 208 | + // Variables declaration - do not modify//GEN-BEGIN:variables |
| 209 | + private final javax.swing.JTextArea additionalBoardsManagerURLs = new javax.swing.JTextArea(); |
| 210 | + // End of variables declaration//GEN-END:variables |
| 211 | + |
| 212 | + public void onOk(ActionListener listener) { |
| 213 | + this.onOkListener = listener; |
| 214 | + } |
| 215 | +} |
0 commit comments