Skip to content

Commit

Permalink
Fixes java-decompiler#48, adds "UI > Main window > Single instance" t…
Browse files Browse the repository at this point in the history
…o preferences
  • Loading branch information
emmanue1 committed Jun 24, 2015
1 parent 77f1f1c commit d71bed5
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 8 deletions.
22 changes: 18 additions & 4 deletions app/src/main/groovy/jd/gui/App.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,40 @@ package jd.gui

import groovy.swing.SwingBuilder
import jd.gui.service.configuration.ConfigurationPersisterService
import jd.gui.util.net.InterProcessCommunications

import javax.swing.JOptionPane

import jd.gui.controller.MainController

class App {
static final String SINGLE_INSTANCE = 'UIMainWindowPreferencesProvider.singleInstance'

static MainController controller

static void main(String[] args) {
if (args.contains("-h")) {
JOptionPane.showMessageDialog(null, """Usage: jd-gui [option] [input-file] ...
Option:
-h\tshow this help message and exit""", Constants.APP_NAME)
JOptionPane.showMessageDialog(null, "Usage: jd-gui [option] [input-file] ...\n\nOption:\n -h Show this help message and exit", Constants.APP_NAME, JOptionPane.INFORMATION_MESSAGE)
} else {
// Load preferences
def persister = ConfigurationPersisterService.instance.get()
def configuration = persister.load()
addShutdownHook { persister.save(configuration) }

if ('true'.equals(configuration.preferences.get(SINGLE_INSTANCE))) {
def ipc = new InterProcessCommunications()

try {
ipc.listen { String[] receivedArgs ->
controller.openFiles(receivedArgs.collect { new File(it) })
}
} catch (Exception notTheFirstInstanceException) {
// Send args to main windows and exit
ipc.send(args)
System.exit(0)
}
}

// Create SwingBuilder, set look and feel
def swing = new SwingBuilder()
swing.lookAndFeel(configuration.lookAndFeel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import jd.gui.spi.SourceSaver
import jd.gui.spi.TreeNodeFactory
import jd.gui.spi.TypeFactory
import jd.gui.spi.UriLoader
import jd.gui.util.UriUtil
import jd.gui.util.net.UriUtil

import javax.swing.Icon
import javax.swing.JComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import jd.gui.api.feature.IndexesChangeListener
import jd.gui.api.model.Container
import jd.gui.api.model.Indexes
import jd.gui.model.configuration.Configuration
import jd.gui.util.UriUtil
import jd.gui.util.net.UriUtil
import jd.gui.view.OpenTypeView

import java.awt.Cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import jd.gui.api.feature.IndexesChangeListener
import jd.gui.api.model.Container
import jd.gui.api.model.Indexes
import jd.gui.model.configuration.Configuration
import jd.gui.util.UriUtil
import jd.gui.util.net.UriUtil
import jd.gui.view.OpenTypeHierarchyView

import java.awt.Cursor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2008-2015 Emmanuel Dupuy
* This program is made available under the terms of the GPLv3 License.
*/

package jd.gui.service.preferencespanel

import jd.gui.spi.PreferencesPanel

import javax.swing.*
import java.awt.*

/**
* JTabbedPane.WRAP_TAB_LAYOUT is not supported by Aqua L&F.
* This panel is not activated on Mac OSX.
*/
class UISingleInstancePreferencesProvider extends JPanel implements PreferencesPanel {

static final String SINGLE_INSTANCE = 'UIMainWindowPreferencesProvider.singleInstance'

JCheckBox singleInstanceTabsCheckBox

UISingleInstancePreferencesProvider() {
super(new GridLayout(0,1))

singleInstanceTabsCheckBox = new JCheckBox('Single instance')

add(singleInstanceTabsCheckBox)
}

// --- PreferencesPanel --- //
String getPreferencesGroupTitle() { 'User Interface' }
String getPreferencesPanelTitle() { 'Main window' }

public void init(Color errorBackgroundColor) {}

public boolean isActivated() {
System.getProperty('os.name').toLowerCase().contains('mac os') == false
}

void loadPreferences(Map<String, String> preferences) {
singleInstanceTabsCheckBox.selected = 'true'.equals(preferences.get(SINGLE_INSTANCE))
}

void savePreferences(Map<String, String> preferences) {
preferences.put(SINGLE_INSTANCE, Boolean.toString(singleInstanceTabsCheckBox.selected))
}

boolean arePreferencesValid() { true }

void addPreferencesChangeListener(PreferencesPanel.PreferencesPanelChangeListener listener) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2008-2015 Emmanuel Dupuy
* This program is made available under the terms of the GPLv3 License.
*/

package jd.gui.util.net

class InterProcessCommunications {

protected static final int PORT = 2015_6

void listen(Closure closure) throws Exception {
def listener = new ServerSocket(PORT)

new Thread().start {
while (true) {
listener.accept().withCloseable { Socket socket ->
closure(new ObjectInputStream(socket.inputStream).readObject())
}
}
}
}

void send(Object obj) {
new Socket(InetAddress.localHost, PORT).withCloseable { Socket socket ->
new ObjectOutputStream(socket.outputStream).writeObject(obj)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This program is made available under the terms of the GPLv3 License.
*/

package jd.gui.util
package jd.gui.util.net

import jd.gui.api.API
import jd.gui.api.model.Container
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
jd.gui.service.preferencespanel.UISingleInstancePreferencesProvider
jd.gui.service.preferencespanel.UITabsPreferencesProvider

0 comments on commit d71bed5

Please sign in to comment.