1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Inverted MVC logic on installer to get a cleaner code.

Part of the control logic was moved from listeners to GUI class methods.
This goes against a pure MVC but makes the code much more cleaner.
This commit is contained in:
Cristian Maglie 2014-05-16 01:07:35 +02:00 committed by Federico Fissore
parent cf058c37ba
commit 5d1e4e3f70
5 changed files with 88 additions and 157 deletions

View File

@ -64,15 +64,14 @@ import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.ContributionsIndex;
import cc.arduino.packages.contributions.ContributionInstaller;
import cc.arduino.packages.contributions.ContributionsIndexer;
@SuppressWarnings("serial")
public class ContributionManagerUI extends JDialog {
private FilterJTextField filterField;
private ContributionManagerUIListener listener = null;
private JLabel categoryLabel;
private JComboBox categoryChooser;
private Component categoryStrut1;
@ -92,8 +91,9 @@ public class ContributionManagerUI extends JDialog {
private String category;
private String[] filters;
public ContributionManagerUI(Frame parent) {
public ContributionManagerUI(Frame parent, ContributionsIndexer indexer) {
super(parent, "Boards Manager", Dialog.ModalityType.APPLICATION_MODAL);
setResizable(true);
Container pane = getContentPane();
@ -144,23 +144,12 @@ public class ContributionManagerUI extends JDialog {
contribTable = new JTable(contribModel);
contribTable.setTableHeader(null);
// contribTable.getTableHeader().setEnabled(false);
// contribTable.setRowSelectionAllowed(false);
contribTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
contribTable.setColumnSelectionAllowed(false);
contribTable.setDragEnabled(false);
contribTable.setIntercellSpacing(new Dimension(0, 1));
contribTable.setShowVerticalLines(false);
// contribTable.addMouseListener(new MouseAdapter() {
// @Override
// public void mousePressed(MouseEvent e) {
// if (listener == null)
// return;
// Point point = e.getPoint();
// int row = contribTable.rowAtPoint(point);
// int col = contribTable.columnAtPoint(point);
// }
// });
{
TableColumnModel tcm = contribTable.getColumnModel();
TableColumn col = tcm.getColumn(DESCRIPTION_COL);
@ -168,16 +157,12 @@ public class ContributionManagerUI extends JDialog {
cellEditor = new ContributedPlatformTableCell() {
@Override
protected void onInstall(ContributedPlatform selectedPlatform) {
if (listener == null)
return;
listener.onInstall(selectedPlatform);
onInstallPressed(selectedPlatform);
}
@Override
protected void onRemove(ContributedPlatform installedPlatform) {
if (listener == null)
return;
listener.onRemove(installedPlatform);
onRemovePressed(installedPlatform);
}
};
col.setCellEditor(cellEditor);
@ -205,8 +190,7 @@ public class ContributionManagerUI extends JDialog {
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (listener != null)
listener.onCancelPressed();
onCancelPressed();
}
});
@ -214,8 +198,7 @@ public class ContributionManagerUI extends JDialog {
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (listener != null)
listener.onUpdatePressed();
onUpdatePressed();
}
});
@ -255,10 +238,18 @@ public class ContributionManagerUI extends JDialog {
});
}
});
}
public void setListener(ContributionManagerUIListener listener) {
this.listener = listener;
contribModel.setIndex(indexer.getIndex());
setCategories(indexer.getIndex().getCategories());
// Create installer with his dialog
installer = new ContributionInstaller(indexer);
installer.setListener(new ContributionInstaller.Listener() {
@Override
public void onProgress(double progress, String message) {
setProgress((int) progress, message);
}
});
}
public void setCategories(Collection<String> categories) {
@ -280,11 +271,6 @@ public class ContributionManagerUI extends JDialog {
categoryStrut3.setVisible(show);
}
public void setContributions(ContributionsIndex index) {
contribModel.setIndex(index);
setCategories(index.getCategories());
}
public void setProgressVisible(boolean visible) {
progressBox.setVisible(visible);
@ -318,4 +304,58 @@ public class ContributionManagerUI extends JDialog {
if (text != null)
progressBar.setString(text);
}
public void onUpdatePressed() {
// TODO Auto-generated method stub
System.out.println("Update pressed");
}
/*
* Installer methods follows
*/
private ContributionInstaller installer;
private Thread installerThread = null;
public void onCancelPressed() {
if (installerThread != null)
installerThread.interrupt();
}
public void onInstallPressed(final ContributedPlatform platform) {
installerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
setProgressVisible(true);
installer.install(platform);
} catch (Exception e) {
// TODO Show ERROR
e.printStackTrace();
} finally {
setProgressVisible(false);
}
}
});
installerThread.start();
}
public void onRemovePressed(final ContributedPlatform platform) {
installerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
setProgressVisible(true);
installer.remove(platform);
} catch (Exception e) {
// TODO Show ERROR
e.printStackTrace();
} finally {
setProgressVisible(false);
}
}
});
installerThread.start();
}
}

View File

@ -1,43 +0,0 @@
/*
* This file is part of Arduino.
*
* Copyright 2014 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package cc.arduino.packages.contributions.ui;
import cc.arduino.packages.contributions.ContributedPlatform;
public interface ContributionManagerUIListener {
void onInstall(ContributedPlatform selected);
void onRemove(ContributedPlatform selected);
void onCancelPressed();
void onUpdatePressed();
}

View File

@ -23,11 +23,7 @@
package processing.app;
import cc.arduino.packages.DiscoveryManager;
import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.ContributionInstaller;
import cc.arduino.packages.contributions.ContributionInstaller.Listener;
import cc.arduino.packages.contributions.ui.ContributionManagerUI;
import cc.arduino.packages.contributions.ui.ContributionManagerUIListener;
import cc.arduino.view.SplashScreenHelper;
import processing.app.debug.TargetBoard;
import processing.app.debug.TargetPackage;
@ -1114,73 +1110,7 @@ public class Base {
private void openInstallBoardDialog() {
// Create dialog for contribution manager
final ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor);
final Listener installerListener = new ContributionInstaller.Listener() {
@Override
public void onProgress(double progress, String message) {
managerUI.setProgress((int) progress, message);
}
};
managerUI.setListener(new ContributionManagerUIListener() {
@Override
public void onUpdatePressed() {
// TODO Auto-generated method stub
System.out.println("Update pressed");
}
Thread task = null;
@Override
public void onCancelPressed() {
if (task != null)
task.interrupt();
}
@Override
public void onInstall(final ContributedPlatform platform) {
final ContributionInstaller installer = new ContributionInstaller(BaseNoGui.indexer);
installer.setListener(installerListener);
task = new Thread(new Runnable() {
@Override
public void run() {
try {
managerUI.setProgressVisible(true);
installer.install(platform);
} catch (Exception e) {
// TODO Show ERROR
e.printStackTrace();
} finally {
managerUI.setProgressVisible(false);
}
}
});
task.start();
}
@Override
public void onRemove(final ContributedPlatform platform) {
// Create installer with his dialog
final ContributionInstaller installer = new ContributionInstaller(BaseNoGui.indexer);
installer.setListener(installerListener);
task = new Thread(new Runnable() {
@Override
public void run() {
try {
managerUI.setProgressVisible(true);
installer.remove(platform);
} catch (Exception e) {
// TODO Show ERROR
e.printStackTrace();
} finally {
managerUI.setProgressVisible(false);
}
}
});
task.start();
}
});
managerUI.setContributions(BaseNoGui.indexer.getIndex());
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor, BaseNoGui.indexer);
managerUI.setVisible(true);
}

View File

@ -48,15 +48,16 @@ import com.fasterxml.jackson.module.mrbean.MrBeanModule;
public class ContributionsIndexer {
private File preferencesFolder;
private File packagesFolder;
private File stagingFolder;
private File indexFile;
private ContributionsIndex index;
public ContributionsIndexer(File _preferencesFolder) {
preferencesFolder = _preferencesFolder;
public ContributionsIndexer(File preferencesFolder) {
packagesFolder = new File(preferencesFolder, "packages");
stagingFolder = new File(preferencesFolder, "staging");
stagingFolder = new File(preferencesFolder, "staging" + File.separator +
"packages");
indexFile = new File(preferencesFolder, "package_index.json");
}
// public static void main(String args[]) throws Exception {
@ -75,7 +76,7 @@ public class ContributionsIndexer {
public void parseIndex() throws JsonParseException, IOException {
// Parse index file
parseIndex(new File(preferencesFolder, "package_index.json"));
parseIndex(indexFile);
List<ContributedPackage> packages = index.getPackages();
for (ContributedPackage pack : packages) {
@ -224,10 +225,6 @@ public class ContributionsIndexer {
return index;
}
public File getPreferencesFolder() {
return preferencesFolder;
}
public File getPackagesFolder() {
return packagesFolder;
}
@ -236,4 +233,8 @@ public class ContributionsIndexer {
return stagingFolder;
}
public File getIndexFile() {
return indexFile;
}
}

View File

@ -586,6 +586,9 @@ public class BaseNoGui {
static public void initPackages() throws Exception {
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
if (!indexer.getIndexFile().isFile())
// TODO: run first setup
;
indexer.parseIndex();
indexer.syncWithFilesystem();
System.out.println(indexer);