1 22 23 package org.gjt.sp.jedit.options; 24 25 import javax.swing.*; 26 import javax.swing.border.*; 27 import java.awt.*; 28 import java.awt.event.*; 29 import java.util.*; 30 import java.util.List ; 31 import java.io.*; 32 33 import org.gjt.sp.jedit.*; 34 import org.gjt.sp.jedit.io.VFSManager; 35 import org.gjt.sp.jedit.pluginmgr.*; 36 import org.gjt.sp.util.*; 37 38 43 public class PluginManagerOptionPane extends AbstractOptionPane 44 { 45 public PluginManagerOptionPane() 47 { 48 super("plugin-manager"); 49 } 51 protected void _init() 53 { 54 setLayout(new BorderLayout()); 55 56 JLabel locationLabel = new JLabel(jEdit.getProperty( 57 "options.plugin-manager.location")); 58 59 mirrorLabel = new JLabel(); 60 updateMirrorLabel(); 61 62 if(jEdit.getSettingsDirectory() != null) 63 { 64 settingsDir = new JRadioButton(jEdit.getProperty( 65 "options.plugin-manager.settings-dir")); 66 settingsDir.setToolTipText(MiscUtilities.constructPath( 67 jEdit.getSettingsDirectory(),"jars")); 68 } 69 JRadioButton appDir = new JRadioButton(jEdit.getProperty( 70 "options.plugin-manager.app-dir")); 71 appDir.setToolTipText(MiscUtilities.constructPath( 72 jEdit.getJEditHome(),"jars")); 73 74 miraList = new JList(miraModel = new MirrorModel()); 75 miraList.setSelectionModel(new SingleSelectionModel()); 76 77 78 add(BorderLayout.NORTH,mirrorLabel); 79 add(BorderLayout.CENTER,new JScrollPane(miraList)); 80 81 JPanel buttonPanel = new JPanel(); 82 buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); 83 84 buttonPanel.add(Box.createVerticalStrut(6)); 85 86 87 updateMirrors = new JButton(jEdit.getProperty( 88 "options.plugin-manager.updateMirrors")); 89 updateMirrors.addActionListener(new ActionHandler()); 90 updateMirrors.setEnabled(false); 91 VFSManager.runInWorkThread(new UpdateMirrorsThread(false)); 92 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 93 panel.add(updateMirrors); 94 panel.add(updateStatus); 95 panel.setAlignmentX(Component.LEFT_ALIGNMENT); 96 buttonPanel.add(panel); 97 98 buttonPanel.add(Box.createVerticalStrut(6)); 99 100 101 downloadSource = new JCheckBox(jEdit.getProperty( 102 "options.plugin-manager.downloadSource")); 103 downloadSource.setSelected(jEdit.getBooleanProperty("plugin-manager.downloadSource")); 104 downloadSource.setAlignmentX(Component.LEFT_ALIGNMENT); 105 buttonPanel.add(downloadSource); 106 107 buttonPanel.add(Box.createVerticalStrut(6)); 108 109 110 deleteDownloads = new JCheckBox(jEdit.getProperty( 111 "options.plugin-manager.deleteDownloads")); 112 deleteDownloads.setSelected(jEdit.getBooleanProperty("plugin-manager.deleteDownloads")); 113 deleteDownloads.setAlignmentX(Component.LEFT_ALIGNMENT); 114 buttonPanel.add(deleteDownloads); 115 116 buttonPanel.add(Box.createVerticalStrut(6)); 117 118 119 ButtonGroup locGrp = new ButtonGroup(); 120 if(jEdit.getSettingsDirectory() != null) 121 locGrp.add(settingsDir); 122 locGrp.add(appDir); 123 JPanel locPanel = new JPanel(); 124 locPanel.setBorder(new EmptyBorder(3,12,0,0)); 125 locPanel.setLayout(new BoxLayout(locPanel,BoxLayout.Y_AXIS)); 126 if(jEdit.getSettingsDirectory() != null) 127 { 128 locPanel.add(settingsDir); 129 locPanel.add(Box.createVerticalStrut(3)); 130 } 131 locPanel.add(appDir); 132 locationLabel.setAlignmentX(Component.LEFT_ALIGNMENT); 133 locPanel.setAlignmentX(Component.LEFT_ALIGNMENT); 134 buttonPanel.add(locationLabel); 135 buttonPanel.add(locPanel); 136 137 buttonPanel.add(Box.createGlue()); 138 add(BorderLayout.SOUTH,buttonPanel); 139 140 if (jEdit.getBooleanProperty("plugin-manager.installUser") 141 && jEdit.getSettingsDirectory() != null) 142 settingsDir.setSelected(true); 143 else 144 appDir.setSelected(true); 145 } 147 protected void _save() 149 { 150 jEdit.setBooleanProperty("plugin-manager.installUser", 151 settingsDir != null && settingsDir.isSelected()); 152 jEdit.setBooleanProperty("plugin-manager.downloadSource",downloadSource.isSelected()); 153 jEdit.setBooleanProperty("plugin-manager.deleteDownloads",deleteDownloads.isSelected()); 154 155 if(miraList.getSelectedIndex() != -1) 156 { 157 String currentMirror = miraModel.getID(miraList.getSelectedIndex()); 158 String previousMirror = jEdit.getProperty("plugin-manager.mirror.id"); 159 160 if (!previousMirror.equals(currentMirror)) 161 { 162 jEdit.setProperty("plugin-manager.mirror.id",currentMirror); 163 jEdit.setProperty("plugin-manager.mirror.name",(String ) miraModel.getElementAt(miraList.getSelectedIndex())); 164 updateMirrorLabel(); 165 } 167 } 168 } 170 172 private JLabel mirrorLabel; 174 175 private JRadioButton settingsDir; 176 private JCheckBox downloadSource; 177 private JCheckBox deleteDownloads; 178 179 private MirrorModel miraModel; 180 private JList miraList; 181 182 private JButton updateMirrors; 183 184 private final JLabel updateStatus = new JLabel(); 185 187 private void updateMirrorLabel() 189 { 190 String currentMirror = jEdit.getProperty("plugin-manager.mirror.id"); 191 String mirrorName; 192 if (currentMirror.equals(MirrorList.Mirror.NONE)) 193 { 194 mirrorName = "Plugin Central default"; 195 } 196 else 197 { 198 mirrorName = jEdit.getProperty("plugin-manager.mirror.name"); 199 if (mirrorName == null) mirrorName = currentMirror; 200 } 201 mirrorLabel.setText(jEdit.getProperty( 202 "options.plugin-manager.mirror") + ' ' + mirrorName); 203 } 205 207 static class MirrorModel extends AbstractListModel 209 { 210 private List <MirrorList.Mirror> mirrors; 211 212 MirrorModel() 213 { 214 mirrors = new ArrayList<MirrorList.Mirror>(); 215 } 216 217 public String getID(int index) 218 { 219 return mirrors.get(index).id; 220 } 221 222 public int getSize() 223 { 224 return mirrors.size(); 225 } 226 227 public Object getElementAt(int index) 228 { 229 MirrorList.Mirror mirror = mirrors.get(index); 230 if(mirror.id.equals(MirrorList.Mirror.NONE)) 231 return jEdit.getProperty("options.plugin-manager.none"); 232 else 233 return mirror.continent+": "+mirror.description+" ("+mirror.location+')'; 234 } 235 236 public void setList(List <MirrorList.Mirror> mirrors) 237 { 238 this.mirrors = mirrors; 239 fireContentsChanged(this,0,mirrors.size() - 1); 240 } 241 } 243 static class SingleSelectionModel extends DefaultListSelectionModel 245 { 246 SingleSelectionModel() 247 { 248 setSelectionMode(SINGLE_SELECTION); 249 } 250 251 public void removeSelectionInterval(int index0, int index1) {} 252 } 254 class ActionHandler implements ActionListener 256 { 257 public void actionPerformed(ActionEvent evt) 258 { 259 updateMirrors.setEnabled(false); 260 updateStatus.setText(jEdit.getProperty("options.plugin-manager.workthread")); 261 VFSManager.runInWorkThread(new UpdateMirrorsThread(true)); 262 } 263 } 265 275 class UpdateMirrorsThread extends WorkRequest 276 { 277 private boolean download; 278 279 UpdateMirrorsThread(boolean download) 280 { 281 this.download = download; 282 } 283 284 public void run() 286 { 287 try 288 { 289 setStatus(jEdit.getProperty("options.plugin-manager.workthread")); 290 setMaximum(3); 291 setValue(0); 292 293 final List <MirrorList.Mirror> mirrors = new ArrayList<MirrorList.Mirror>(); 294 try 295 { 296 MirrorList mirrorList = new MirrorList(download, this); 297 if (download) 298 saveMirrorList(mirrorList.xml); 299 300 mirrors.addAll(mirrorList.mirrors); 301 } 302 catch (Exception ex) 303 { 304 if (download) 305 { 306 Log.log(Log.ERROR,this,ex); 307 GUIUtilities.error(PluginManagerOptionPane.this, 308 "ioerror",new String [] { ex.toString() }); 309 } 310 } 311 312 SwingUtilities.invokeLater(new Runnable () 313 { 314 public void run() 315 { 316 miraModel.setList(mirrors); 317 318 String id = jEdit.getProperty("plugin-manager.mirror.id"); 319 int size = miraModel.getSize(); 320 for (int i=0; i < size; i++) 321 { 322 if (size == 1 || miraModel.getID(i).equals(id)) 323 { 324 miraList.setSelectedIndex(i); 325 break; 326 } 327 } 328 } 329 }); 330 331 setValue(3); 332 } 333 finally 334 { 335 updateMirrors.setEnabled(true); 336 updateStatus.setText(null); 337 } 338 } 340 private void saveMirrorList(String xml) 342 { 343 String settingsDirectory = jEdit.getSettingsDirectory(); 344 if(settingsDirectory == null) 345 return; 346 347 File mirrorList = new File(MiscUtilities.constructPath( 348 settingsDirectory,"mirrorList.xml")); 349 OutputStream out = null; 350 351 try 352 { 353 out = new BufferedOutputStream(new FileOutputStream(mirrorList)); 354 IOUtilities.copyStream(null, new ByteArrayInputStream(xml.getBytes()), out, false); 355 } 356 catch (IOException e) 357 { 358 Log.log(Log.ERROR,this, "Unable to write cached mirror list : " + mirrorList); 359 } 360 finally 361 { 362 IOUtilities.closeQuietly(out); 363 } 364 } } } 367 | Popular Tags |