diff options
Diffstat (limited to 'dozentenmodul')
-rw-r--r-- | dozentenmodul/pom.xml | 505 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/gui/GuiManager.java | 87 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/gui/core/LoginComposite.java | 175 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/org/eclipse/wb/swt/SWTResourceManager.java | 447 |
4 files changed, 503 insertions, 711 deletions
diff --git a/dozentenmodul/pom.xml b/dozentenmodul/pom.xml index b87d729d..b4c51dce 100644 --- a/dozentenmodul/pom.xml +++ b/dozentenmodul/pom.xml @@ -1,263 +1,274 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>bwlehrpool</groupId> - <artifactId>dozmod-client</artifactId> - <version>1.0-SNAPSHOT</version> - <packaging>jar</packaging> + <groupId>bwlehrpool</groupId> + <artifactId>dozmod-client</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> - <name>DozMod-Client</name> - <url>http://maven.apache.org</url> + <name>DozMod-Client</name> + <url>http://maven.apache.org</url> - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <maven.build.timestamp.format>yy.M.d.Hmm</maven.build.timestamp.format> - </properties> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.build.timestamp.format>yy.M.d.Hmm</maven.build.timestamp.format> + </properties> - <repositories> - <repository> - <id>mltk-repo</id> - <name>mltk repo</name> - <url>http://mltk-services.ruf.uni-freiburg.de:8081/nexus/content/repositories/snapshots/</url> - <releases> - <enabled>true</enabled> - <updatePolicy>always</updatePolicy> - </releases> - <snapshots> - <enabled>true</enabled> - <updatePolicy>always</updatePolicy> - </snapshots> - </repository> - </repositories> + <repositories> + <repository> + <id>mltk-repo</id> + <name>mltk repo</name> + <url>http://mltk-services.ruf.uni-freiburg.de:8081/nexus/content/repositories/snapshots/</url> + <releases> + <enabled>true</enabled> + <updatePolicy>always</updatePolicy> + </releases> + <snapshots> + <enabled>true</enabled> + <updatePolicy>always</updatePolicy> + </snapshots> + </repository> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.1</version> - <configuration> - <source>1.7</source> - <target>1.7</target> - </configuration> - </plugin> - <plugin> - <artifactId>maven-assembly-plugin</artifactId> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>single</goal> - </goals> - </execution> - </executions> - <configuration> - <archive> - <manifest> - <mainClass>App</mainClass> - </manifest> - <manifestEntries> - <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> - </manifestEntries> - </archive> - <descriptorRefs> - <descriptorRef>jar-with-dependencies</descriptorRef> - </descriptorRefs> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-shade-plugin</artifactId> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>shade</goal> - </goals> - <configuration> - <filters> - <filter> - <artifact>*:*</artifact> - <excludes> - <exclude>META-INF/*.SF</exclude> - <exclude>META-INF/*.DSA</exclude> - <exclude>META-INF/*.RSA</exclude> - </excludes> - </filter> - <filter> - <artifact>log4j:*</artifact> - <includes> - <include>**</include> - </includes> - </filter> - <filter> - <artifact>commons-logging:*</artifact> - <includes> - <include>**</include> - </includes> - </filter> - </filters> - <minimizeJar>true</minimizeJar> - <transformers> - <transformer - implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> - <mainClass>App</mainClass> - <manifestEntries> - <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> - </manifestEntries> - </transformer> - </transformers> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - <resources> - <resource> - <directory>src/main/resources</directory> - <includes> - <include>**</include> - </includes> - </resource> - <resource> - <directory>src/main/properties</directory> - <includes> - <include>**/*.properties</include> - </includes> - </resource> - </resources> - </build> - <profiles> - <profile> - <id>mac</id> - <activation> - <os> - <name>mac os x</name> - </os> - </activation> - <properties> - <swt.groupId>org.eclipse.swt.carbon</swt.groupId> - <swt.artifactId>macosx</swt.artifactId> - </properties> - </profile> - <profile> - <id>windows</id> - <activation> - <os> - <family>windows</family> - </os> - </activation> - <properties> - <swt.groupId>org.eclipse.swt.org.eclipse.swt.win32.win32.x86.4.3.swt</swt.groupId> - <swt.artifactId>org.eclipse.swt.win32.win32.x86</swt.artifactId> - </properties> - </profile> - <profile> - <id>linux_64</id> - <activation> - <os> - <name>linux</name> - <arch>amd64</arch> - </os> - </activation> - <properties> - <swt.groupId>org.eclipse.swt.org.eclipse.swt.gtk.linux.x86_64.4.3.swt</swt.groupId> - <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId> - </properties> - </profile> - <profile> - <id>linux_32</id> - <activation> - <os> - <name>linux</name> - <arch>i386</arch> - </os> - </activation> - <properties> - <swt.groupId>org.eclipse.swt.org.eclipse.swt.gtk.linux.x86.4.3.swt</swt.groupId> - <swt.artifactId>org.eclipse.swt.gtk.linux.x86</swt.artifactId> - </properties> - </profile> - </profiles> + <repository> + <id>swt-repo</id> + <url>https://raw.githubusercontent.com/maven-eclipse/swt-repo/master/</url> + </repository> - <dependencies> - <dependency> - <groupId>org.mod4j.org.eclipse.jface</groupId> - <artifactId>text</artifactId> - <version>3.5.0</version> - </dependency> - <dependency> - <groupId>org.apache.thrift</groupId> - <artifactId>libthrift</artifactId> - <version>0.9.1</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - <version>2.4</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>com.toedter</groupId> - <artifactId>jcalendar</artifactId> - <version>1.4</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>net.sourceforge.jdatepicker</groupId> - <artifactId>jdatepicker</artifactId> - <version>1.3.2</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>${swt.groupId}</groupId> - <artifactId>${swt.artifactId}</artifactId> - <version>4.3</version> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>[1.2.10,1.2.20]</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>org.ini4j</groupId> - <artifactId>ini4j</artifactId> - <version>0.5.2</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.5.8</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>org.openslx.bwlp</groupId> - <artifactId>master-sync-shared</artifactId> - <version>1.1-SNAPSHOT</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>3.8.1</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.openslx.ecp</groupId> - <artifactId>ecp-client-lean</artifactId> - <version>0.0.2-SNAPSHOT</version> - </dependency> + + </repositories> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.7</source> + <target>1.7</target> + </configuration> + </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + <configuration> + <archive> + <manifest> + <mainClass>App</mainClass> + </manifest> + <manifestEntries> + <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> + </manifestEntries> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.DSA</exclude> + <exclude>META-INF/*.RSA</exclude> + </excludes> + </filter> + <filter> + <artifact>log4j:*</artifact> + <includes> + <include>**</include> + </includes> + </filter> + <filter> + <artifact>commons-logging:*</artifact> + <includes> + <include>**</include> + </includes> + </filter> + </filters> + <minimizeJar>true</minimizeJar> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <mainClass>App</mainClass> + <manifestEntries> + <Version-Timestamp>${maven.build.timestamp}</Version-Timestamp> + </manifestEntries> + </transformer> + </transformers> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + <resources> + <resource> + <directory>src/main/resources</directory> + <includes> + <include>**</include> + </includes> + </resource> + <resource> + <directory>src/main/properties</directory> + <includes> + <include>**/*.properties</include> + </includes> + </resource> + </resources> + </build> + + <profiles> + <profile> + <id>mac</id> + <activation> + <os> + <name>mac os x</name> + </os> + </activation> + <properties> + <swt.groupId>org.eclipse.swt.carbon</swt.groupId> + <swt.artifactId>macosx</swt.artifactId> + </properties> + </profile> + <profile> + <id>windows</id> + <activation> + <os> + <family>windows</family> + </os> + </activation> + <properties> + <swt.groupId>org.eclipse.swt.org.eclipse.swt.win32.win32.x86.4.3.swt</swt.groupId> + <swt.artifactId>org.eclipse.swt.win32.win32.x86</swt.artifactId> + </properties> + </profile> + <profile> + <id>linux_64</id> + <activation> + <os> + <name>linux</name> + <arch>amd64</arch> + </os> + </activation> + <properties> + + + + <swt.groupId>org.eclipse.swt</swt.groupId> + <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId> + </properties> + </profile> + <profile> + <id>linux_32</id> + <activation> + <os> + <name>linux</name> + <arch>i386</arch> + </os> + </activation> + <properties> + <swt.groupId>org.eclipse.swt.org.eclipse.swt.gtk.linux.x86.4.3.swt</swt.groupId> + <swt.artifactId>org.eclipse.swt.gtk.linux.x86</swt.artifactId> + </properties> + </profile> + </profiles> + + <dependencies> + <dependency> + <groupId>org.mod4j.org.eclipse.jface</groupId> + <artifactId>text</artifactId> + <version>3.5.0</version> + </dependency> + <dependency> + <groupId>org.apache.thrift</groupId> + <artifactId>libthrift</artifactId> + <version>0.9.1</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.4</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>com.toedter</groupId> + <artifactId>jcalendar</artifactId> + <version>1.4</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>net.sourceforge.jdatepicker</groupId> + <artifactId>jdatepicker</artifactId> + <version>1.3.2</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>${swt.groupId}</groupId> + <artifactId>${swt.artifactId}</artifactId> + <version>4.4</version> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>[1.2.10,1.2.20]</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.ini4j</groupId> + <artifactId>ini4j</artifactId> + <version>0.5.2</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.5.8</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.openslx.bwlp</groupId> + <artifactId>master-sync-shared</artifactId> + <version>1.1-SNAPSHOT</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.openslx.ecp</groupId> + <artifactId>ecp-client-lean</artifactId> + <version>0.0.2-SNAPSHOT</version> + </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> <scope>compile</scope> </dependency> - </dependencies> + </dependencies> </project> diff --git a/dozentenmodul/src/main/java/gui/GuiManager.java b/dozentenmodul/src/main/java/gui/GuiManager.java index 41d5c711..d3904238 100644 --- a/dozentenmodul/src/main/java/gui/GuiManager.java +++ b/dozentenmodul/src/main/java/gui/GuiManager.java @@ -1,31 +1,84 @@ package gui; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; public abstract class GuiManager { + static Shell mainShell; + static Composite contentComposite; + static Display display; - private static Display _display; - private static Shell _mainShell; - private static Shell _containerShell; + /** + * Add a new composite with content to the main Shell + * @param The composite to add, should be a GUI + */ + public static void addContent( Composite contentComposite ){ + removeContent(); + + GuiManager.contentComposite = contentComposite; + mainShell.layout(); + + } + + /** + * Remove the current content of the main shell + */ + private static void removeContent() { + if(contentComposite != null) { + GuiManager.contentComposite.dispose(); + + } + System.out.println(mainShell.toString()); + } + + + public static Display getDisplay(){ + return display; + } + public static void initGui() { - // init display, shell - _display = new Display(); - _mainShell = new Shell(_display); + display = new Display(); + mainShell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); + + Menu menuBar = new Menu(mainShell, SWT.BAR); + MenuItem cascadeFileMenu = new MenuItem(menuBar, SWT.CASCADE); + cascadeFileMenu.setText("&File"); + + Menu fileMenu = new Menu(mainShell, SWT.DROP_DOWN); + cascadeFileMenu.setMenu(fileMenu); + + MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); + exitItem.setText("&Exit"); + exitItem.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + mainShell.getDisplay().dispose(); + System.exit(0); + } + }); + + mainShell.setText("Dozmod"); + mainShell.setMenuBar(menuBar); + mainShell.setLayout(new FillLayout()); + + addContent(new gui.core.LoginComposite(mainShell)); + - // add static gui elements - _containerShell = _mainShell; + mainShell.pack(); + mainShell.open(); - // pack (aka size widgets) and open - _mainShell.pack(); - _mainShell.open(); - // main loop - while (!_mainShell.isDisposed()) { - if (!_display.readAndDispatch()) - _display.sleep(); + while (!mainShell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); } - _display.dispose(); } -}
\ No newline at end of file +} diff --git a/dozentenmodul/src/main/java/gui/core/LoginComposite.java b/dozentenmodul/src/main/java/gui/core/LoginComposite.java new file mode 100644 index 00000000..38449b2f --- /dev/null +++ b/dozentenmodul/src/main/java/gui/core/LoginComposite.java @@ -0,0 +1,175 @@ +package gui.core; +import gui.GuiManager; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + + +public class LoginComposite extends Composite { + + private Image _titleImage; + private Text _usernameText; + private Text _passwordText; + /** + * Create a new login composite + * @param mainShell The shell it should be added to + + */ + public LoginComposite(final Shell mainShell) { + super(mainShell, SWT.NO_BACKGROUND); + mainShell.setText("Dozmod - Login"); + + // Left authentication selection and right loginmask + GridLayout gridLayout = new GridLayout(2, true); + this.setLayout(gridLayout); + + //Load the needed Picture + loadImage(); + + Label titlePicture = new Label(this, SWT.NONE); + titlePicture.setImage(_titleImage); + GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); + gridData.horizontalSpan = 2; + gridData.horizontalAlignment = SWT.CENTER; + titlePicture.setLayoutData(gridData); + + // Group for the authentication method. + Group authGroup = new Group(this, SWT.NONE); + authGroup.setText("Authentifizierungsart"); + gridLayout = new GridLayout(); + gridLayout.numColumns = 1; + authGroup.setLayout(gridLayout); + gridData = new GridData(GridData.FILL, GridData.CENTER, false, false); + gridData.heightHint = 150; + authGroup.setLayoutData(gridData); + + // Add the radio buttons + Button[] authButtons = new Button[3]; + + authButtons[0] = new Button(authGroup, SWT.RADIO); + authButtons[0].setSelection(true); + authButtons[0].setText("Authentifizierung über bwIDM"); + + authButtons[1] = new Button(authGroup, SWT.RADIO); + authButtons[1].setText("Test-Zugang mit festem Benutzernamen"); + + authButtons[2] = new Button(authGroup, SWT.RADIO); + authButtons[2].setText("Direkte Verbindung zum Satteliten"); + + + // Group for the login mask + final Group loginGroup = new Group(this, SWT.NONE); + loginGroup.setText("Zugangsdaten"); + gridLayout = new GridLayout(); + gridLayout.numColumns = 2; + loginGroup.setLayout(gridLayout); + gridData = new GridData(GridData.FILL, GridData.CENTER, true, false); + gridData.heightHint = 150; + loginGroup.setLayoutData(gridData); + + + final Label IdPText = new Label(loginGroup, SWT.NONE); + IdPText.setText("IdP:"); + + + final Combo IdPCombo = new Combo(loginGroup, SWT.DROP_DOWN | SWT.READ_ONLY); + + + IdPCombo.add("Ubuntu"); + IdPCombo.add("Fedora"); + IdPCombo.add("Mandriva"); + IdPCombo.add("Red Hat"); + IdPCombo.add("Mint"); + + + + new Label(loginGroup, SWT.NONE).setText("Benutzername:"); + _usernameText = new Text(loginGroup, SWT.SINGLE | SWT.BORDER); + _usernameText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); + + new Label(loginGroup, SWT.NONE).setText("Passwort:"); + _passwordText = new Text(loginGroup, SWT.SINGLE | SWT.BORDER); + _passwordText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); + Button loginButton = new Button(loginGroup, SWT.PUSH); + loginButton.setText("Login"); + Button saveUsernameCheck = new Button(loginGroup, SWT.CHECK); + saveUsernameCheck.setText("Benutzername speichern"); + + // Actions of the login button + loginButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + System.out.println("BAM!"); + //loginComposite.dispose(); + mainShell.setText("Login?"); + + + } + }); + + // For save username checkbox. + saveUsernameCheck.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + System.out.println("Checkboxtoggle"); + } + }); + + // Selecting the "Authentifizierung über bwIDM" radio button + authButtons[0].addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + IdPText.setVisible(true); + IdPCombo.setVisible(true); + } + }); + + // Selecting the "Test-Zugang über bwIDM" radio button + authButtons[1].addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + IdPText.setVisible(false); + IdPCombo.setVisible(false); + + } + }); + + authButtons[2].setEnabled(false); + // Selecting the "Direkte Verbindung zum Satteliten" radio button + authButtons[2].addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + IdPText.setVisible(false); + IdPCombo.setVisible(false); + } + }); + } + + + + private void loadImage(){ + try { + _titleImage = new Image(GuiManager.getDisplay() , "resources/Logo_bwLehrpool.png"); + ImageData imgData = _titleImage.getImageData(); + imgData = imgData.scaledTo(imgData.width/5, imgData.height/5); + _titleImage = new Image(GuiManager.getDisplay(), imgData); + } catch (Exception e) { + System.out.println("Cannot load image"); + System.out.println(e.getMessage()); + } + } + + +} diff --git a/dozentenmodul/src/main/java/org/eclipse/wb/swt/SWTResourceManager.java b/dozentenmodul/src/main/java/org/eclipse/wb/swt/SWTResourceManager.java deleted file mode 100644 index 8b6d4cc3..00000000 --- a/dozentenmodul/src/main/java/org/eclipse/wb/swt/SWTResourceManager.java +++ /dev/null @@ -1,447 +0,0 @@ -/*******************************************************************************
- * Copyright (c) 2011 Google, Inc.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Google, Inc. - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wb.swt; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Cursor; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.FontData; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.eclipse.swt.graphics.RGB; -import org.eclipse.swt.graphics.Rectangle; -import org.eclipse.swt.widgets.Display; - -/** - * Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc. - * <p> - * !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> method to release the - * operating system resources managed by cached objects when those objects and OS resources are no longer - * needed (e.g. on application shutdown) - * <p> - * This class may be freely distributed as part of any application or plugin. - * <p> - * @author scheglov_ke - * @author Dan Rubel - */ -public class SWTResourceManager { - //////////////////////////////////////////////////////////////////////////// - // - // Color - // - //////////////////////////////////////////////////////////////////////////// - private static Map<RGB, Color> m_colorMap = new HashMap<RGB, Color>(); - /** - * Returns the system {@link Color} matching the specific ID. - * - * @param systemColorID - * the ID value for the color - * @return the system {@link Color} matching the specific ID - */ - public static Color getColor(int systemColorID) { - Display display = Display.getCurrent(); - return display.getSystemColor(systemColorID); - } - /** - * Returns a {@link Color} given its red, green and blue component values. - * - * @param r - * the red component of the color - * @param g - * the green component of the color - * @param b - * the blue component of the color - * @return the {@link Color} matching the given red, green and blue component values - */ - public static Color getColor(int r, int g, int b) { - return getColor(new RGB(r, g, b)); - } - /** - * Returns a {@link Color} given its RGB value. - * - * @param rgb - * the {@link RGB} value of the color - * @return the {@link Color} matching the RGB value - */ - public static Color getColor(RGB rgb) { - Color color = m_colorMap.get(rgb); - if (color == null) { - Display display = Display.getCurrent(); - color = new Color(display, rgb); - m_colorMap.put(rgb, color); - } - return color; - } - /** - * Dispose of all the cached {@link Color}'s. - */ - public static void disposeColors() { - for (Color color : m_colorMap.values()) { - color.dispose(); - } - m_colorMap.clear(); - } - //////////////////////////////////////////////////////////////////////////// - // - // Image - // - //////////////////////////////////////////////////////////////////////////// - /** - * Maps image paths to images. - */ - private static Map<String, Image> m_imageMap = new HashMap<String, Image>(); - /** - * Returns an {@link Image} encoded by the specified {@link InputStream}. - * - * @param stream - * the {@link InputStream} encoding the image data - * @return the {@link Image} encoded by the specified input stream - */ - protected static Image getImage(InputStream stream) throws IOException { - try { - Display display = Display.getCurrent(); - ImageData data = new ImageData(stream); - if (data.transparentPixel > 0) { - return new Image(display, data, data.getTransparencyMask()); - } - return new Image(display, data); - } finally { - stream.close(); - } - } - /** - * Returns an {@link Image} stored in the file at the specified path. - * - * @param path - * the path to the image file - * @return the {@link Image} stored in the file at the specified path - */ - public static Image getImage(String path) { - Image image = m_imageMap.get(path); - if (image == null) { - try { - image = getImage(new FileInputStream(path)); - m_imageMap.put(path, image); - } catch (Exception e) { - image = getMissingImage(); - m_imageMap.put(path, image); - } - } - return image; - } - /** - * Returns an {@link Image} stored in the file at the specified path relative to the specified class. - * - * @param clazz - * the {@link Class} relative to which to find the image - * @param path - * the path to the image file, if starts with <code>'/'</code> - * @return the {@link Image} stored in the file at the specified path - */ - public static Image getImage(Class<?> clazz, String path) { - String key = clazz.getName() + '|' + path; - Image image = m_imageMap.get(key); - if (image == null) { - try { - image = getImage(clazz.getResourceAsStream(path)); - m_imageMap.put(key, image); - } catch (Exception e) { - image = getMissingImage(); - m_imageMap.put(key, image); - } - } - return image; - } - private static final int MISSING_IMAGE_SIZE = 10; - /** - * @return the small {@link Image} that can be used as placeholder for missing image. - */ - private static Image getMissingImage() { - Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); - // - GC gc = new GC(image); - gc.setBackground(getColor(SWT.COLOR_RED)); - gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); - gc.dispose(); - // - return image; - } - /** - * Style constant for placing decorator image in top left corner of base image. - */ - public static final int TOP_LEFT = 1; - /** - * Style constant for placing decorator image in top right corner of base image. - */ - public static final int TOP_RIGHT = 2; - /** - * Style constant for placing decorator image in bottom left corner of base image. - */ - public static final int BOTTOM_LEFT = 3; - /** - * Style constant for placing decorator image in bottom right corner of base image. - */ - public static final int BOTTOM_RIGHT = 4; - /** - * Internal value. - */ - protected static final int LAST_CORNER_KEY = 5; - /** - * Maps images to decorated images. - */ - @SuppressWarnings("unchecked") - private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY]; - /** - * Returns an {@link Image} composed of a base image decorated by another image. - * - * @param baseImage - * the base {@link Image} that should be decorated - * @param decorator - * the {@link Image} to decorate the base image - * @return {@link Image} The resulting decorated image - */ - public static Image decorateImage(Image baseImage, Image decorator) { - return decorateImage(baseImage, decorator, BOTTOM_RIGHT); - } - /** - * Returns an {@link Image} composed of a base image decorated by another image. - * - * @param baseImage - * the base {@link Image} that should be decorated - * @param decorator - * the {@link Image} to decorate the base image - * @param corner - * the corner to place decorator image - * @return the resulting decorated {@link Image} - */ - public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) { - if (corner <= 0 || corner >= LAST_CORNER_KEY) { - throw new IllegalArgumentException("Wrong decorate corner"); - } - Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner]; - if (cornerDecoratedImageMap == null) { - cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>(); - m_decoratedImageMap[corner] = cornerDecoratedImageMap; - } - Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage); - if (decoratedMap == null) { - decoratedMap = new HashMap<Image, Image>(); - cornerDecoratedImageMap.put(baseImage, decoratedMap); - } - // - Image result = decoratedMap.get(decorator); - if (result == null) { - Rectangle bib = baseImage.getBounds(); - Rectangle dib = decorator.getBounds(); - // - result = new Image(Display.getCurrent(), bib.width, bib.height); - // - GC gc = new GC(result); - gc.drawImage(baseImage, 0, 0); - if (corner == TOP_LEFT) { - gc.drawImage(decorator, 0, 0); - } else if (corner == TOP_RIGHT) { - gc.drawImage(decorator, bib.width - dib.width, 0); - } else if (corner == BOTTOM_LEFT) { - gc.drawImage(decorator, 0, bib.height - dib.height); - } else if (corner == BOTTOM_RIGHT) { - gc.drawImage(decorator, bib.width - dib.width, bib.height - dib.height); - } - gc.dispose(); - // - decoratedMap.put(decorator, result); - } - return result; - } - /** - * Dispose all of the cached {@link Image}'s. - */ - public static void disposeImages() { - // dispose loaded images - { - for (Image image : m_imageMap.values()) { - image.dispose(); - } - m_imageMap.clear(); - } - // dispose decorated images - for (int i = 0; i < m_decoratedImageMap.length; i++) { - Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i]; - if (cornerDecoratedImageMap != null) { - for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) { - for (Image image : decoratedMap.values()) { - image.dispose(); - } - decoratedMap.clear(); - } - cornerDecoratedImageMap.clear(); - } - } - } - //////////////////////////////////////////////////////////////////////////// - // - // Font - // - //////////////////////////////////////////////////////////////////////////// - /** - * Maps font names to fonts. - */ - private static Map<String, Font> m_fontMap = new HashMap<String, Font>(); - /** - * Maps fonts to their bold versions. - */ - private static Map<Font, Font> m_fontToBoldFontMap = new HashMap<Font, Font>(); - /** - * Returns a {@link Font} based on its name, height and style. - * - * @param name - * the name of the font - * @param height - * the height of the font - * @param style - * the style of the font - * @return {@link Font} The font matching the name, height and style - */ - public static Font getFont(String name, int height, int style) { - return getFont(name, height, style, false, false); - } - /** - * Returns a {@link Font} based on its name, height and style. Windows-specific strikeout and underline - * flags are also supported. - * - * @param name - * the name of the font - * @param size - * the size of the font - * @param style - * the style of the font - * @param strikeout - * the strikeout flag (warning: Windows only) - * @param underline - * the underline flag (warning: Windows only) - * @return {@link Font} The font matching the name, height, style, strikeout and underline - */ - public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) { - String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline; - Font font = m_fontMap.get(fontName); - if (font == null) { - FontData fontData = new FontData(name, size, style); - if (strikeout || underline) { - try { - Class<?> logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); //$NON-NLS-1$ - Object logFont = FontData.class.getField("data").get(fontData); //$NON-NLS-1$ - if (logFont != null && logFontClass != null) { - if (strikeout) { - logFontClass.getField("lfStrikeOut").set(logFont, Byte.valueOf((byte) 1)); //$NON-NLS-1$ - } - if (underline) { - logFontClass.getField("lfUnderline").set(logFont, Byte.valueOf((byte) 1)); //$NON-NLS-1$ - } - } - } catch (Throwable e) { - System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - font = new Font(Display.getCurrent(), fontData); - m_fontMap.put(fontName, font); - } - return font; - } - /** - * Returns a bold version of the given {@link Font}. - * - * @param baseFont - * the {@link Font} for which a bold version is desired - * @return the bold version of the given {@link Font} - */ - public static Font getBoldFont(Font baseFont) { - Font font = m_fontToBoldFontMap.get(baseFont); - if (font == null) { - FontData fontDatas[] = baseFont.getFontData(); - FontData data = fontDatas[0]; - font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD); - m_fontToBoldFontMap.put(baseFont, font); - } - return font; - } - /** - * Dispose all of the cached {@link Font}'s. - */ - public static void disposeFonts() { - // clear fonts - for (Font font : m_fontMap.values()) { - font.dispose(); - } - m_fontMap.clear(); - // clear bold fonts - for (Font font : m_fontToBoldFontMap.values()) { - font.dispose(); - } - m_fontToBoldFontMap.clear(); - } - //////////////////////////////////////////////////////////////////////////// - // - // Cursor - // - //////////////////////////////////////////////////////////////////////////// - /** - * Maps IDs to cursors. - */ - private static Map<Integer, Cursor> m_idToCursorMap = new HashMap<Integer, Cursor>(); - /** - * Returns the system cursor matching the specific ID. - * - * @param id - * int The ID value for the cursor - * @return Cursor The system cursor matching the specific ID - */ - public static Cursor getCursor(int id) { - Integer key = Integer.valueOf(id); - Cursor cursor = m_idToCursorMap.get(key); - if (cursor == null) { - cursor = new Cursor(Display.getDefault(), id); - m_idToCursorMap.put(key, cursor); - } - return cursor; - } - /** - * Dispose all of the cached cursors. - */ - public static void disposeCursors() { - for (Cursor cursor : m_idToCursorMap.values()) { - cursor.dispose(); - } - m_idToCursorMap.clear(); - } - //////////////////////////////////////////////////////////////////////////// - // - // General - // - //////////////////////////////////////////////////////////////////////////// - /** - * Dispose of cached objects and their underlying OS resources. This should only be called when the cached - * objects are no longer needed (e.g. on application shutdown). - */ - public static void dispose() { - disposeColors(); - disposeImages(); - disposeFonts(); - disposeCursors(); - } -}
\ No newline at end of file |