Skip to content

Commit

Permalink
enhance toggle behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
chriba committed Sep 28, 2016
1 parent c3186f6 commit 6d60cd6
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 144 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jabref/collab/FileUpdatePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public void componentClosing() {
manager.unregisterComponent(FileUpdatePanel.NAME);
}

@Override
public String getSidePaneName() {
return NAME;
}

@Override
public int getRescalingWeight() {
return 0;
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,6 @@ private void setupActions() {

});

// The action for toggling the groups interface
actions.put(Actions.TOGGLE_GROUPS, (BaseAction) () -> {
sidePaneManager.toggle("groups");
frame.groupToggle.setSelected(sidePaneManager.isComponentVisible("groups"));
});

actions.put(FindUnlinkedFilesDialog.ACTION_COMMAND, (BaseAction) () -> {
final FindUnlinkedFilesDialog dialog = new FindUnlinkedFilesDialog(frame, frame, BasePanel.this);
dialog.setLocationRelativeTo(frame);
Expand Down Expand Up @@ -1217,7 +1211,7 @@ public void listen(EntryAddedEvent addedEntryEvent) {
}

// Automatically add new entry to the selected group (or set of groups)
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP) && frame.groupToggle.isSelected()) {
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP) && frame.getGroupSelector().getAction().isSelected()) {
final List<BibEntry> entries = Collections.singletonList(addedEntryEvent.getBibEntry());
final TreePath[] selection = frame.getGroupSelector().getGroupsTree().getSelectionPaths();
if (selection != null) {
Expand Down Expand Up @@ -2160,7 +2154,7 @@ public void fileUpdated() {
}
FileUpdatePanel pan = new FileUpdatePanel(BasePanel.this, sidePaneManager,
getBibDatabaseContext().getDatabaseFile().orElse(null), scanner);
sidePaneManager.register(FileUpdatePanel.NAME, pan);
sidePaneManager.register(pan);
sidePaneManager.show(FileUpdatePanel.NAME);
};

Expand All @@ -2185,10 +2179,10 @@ public void cleanUp() {
}
// Check if there is a FileUpdatePanel for this BasePanel being shown. If so,
// remove it:
if (sidePaneManager.hasComponent("fileUpdate")) {
FileUpdatePanel fup = (FileUpdatePanel) sidePaneManager.getComponent("fileUpdate");
if (sidePaneManager.hasComponent(FileUpdatePanel.NAME)) {
FileUpdatePanel fup = (FileUpdatePanel) sidePaneManager.getComponent(FileUpdatePanel.NAME);
if (fup.getPanel() == this) {
sidePaneManager.hideComponent("fileUpdate");
sidePaneManager.hideComponent(FileUpdatePanel.NAME);
}
}
}
Expand Down
51 changes: 16 additions & 35 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ public class JabRefFrame extends JFrame implements OutputPrinter {
// for the name and message strings.

/* References to the toggle buttons in the toolbar */
// the groups interface
public JToggleButton groupToggle;
private JToggleButton previewToggle;
private JToggleButton fetcherToggle;


private final OpenDatabaseAction open = new OpenDatabaseAction(this, true);
private final EditModeAction editModeAction = new EditModeAction();
Expand Down Expand Up @@ -351,11 +347,6 @@ public void actionPerformed(ActionEvent e) {
}
});

private final Action toggleGroups = enableToggle(new GeneralAction(Actions.TOGGLE_GROUPS,
Localization.menuTitle("Toggle groups interface"),
Localization.lang("Toggle groups interface"),
Globals.getKeyPrefs().getKey(KeyBinding.TOGGLE_GROUPS_INTERFACE),
IconTheme.JabRefIcon.TOGGLE_GROUPS.getIcon()));
private final AbstractAction addToGroup = new GeneralAction(Actions.ADD_TO_GROUP, Localization.lang("Add to group") + ELLIPSES);
private final AbstractAction removeFromGroup = new GeneralAction(Actions.REMOVE_FROM_GROUP,
Localization.lang("Remove from group") + ELLIPSES);
Expand Down Expand Up @@ -394,8 +385,6 @@ public void actionPerformed(ActionEvent e) {
Localization.lang("Will write XMP-metadata to the PDFs linked from selected entries."),
Globals.getKeyPrefs().getKey(KeyBinding.WRITE_XMP));

private JMenuItem optMenuItem;

private final AbstractAction openFolder = new GeneralAction(Actions.OPEN_FOLDER,
Localization.menuTitle("Open folder"), Localization.lang("Open folder"),
Globals.getKeyPrefs().getKey(KeyBinding.OPEN_FOLDER));
Expand Down Expand Up @@ -481,7 +470,7 @@ public void actionPerformed(ActionEvent e) {
private PushToApplications pushApplications;

private GeneralFetcher generalFetcher;

private OpenOfficePanel openOfficePanel;
private GroupSelector groupSelector;

private int previousTabCount = -1;
Expand Down Expand Up @@ -662,9 +651,10 @@ public void windowClosing(WindowEvent e) {

currentBasePanel.getPreviewPanel().updateLayout();

groupToggle.setSelected(sidePaneManager.isComponentVisible("groups"));
groupSelector.getAction().setSelected(sidePaneManager.isComponentVisible(GroupSelector.NAME));
previewToggle.setSelected(Globals.prefs.getPreviewPreferences().isPreviewPanelEnabled());
fetcherToggle.setSelected(sidePaneManager.isComponentVisible(generalFetcher.getTitle()));
generalFetcher.getAction().setSelected(sidePaneManager.isComponentVisible(GeneralFetcher.NAME));
openOfficePanel.getAction().setSelected(sidePaneManager.isComponentVisible(OpenOfficePanel.NAME));
Globals.getFocusListener().setFocused(currentBasePanel.getMainTable());
setWindowTitle();
editModeAction.initName();
Expand Down Expand Up @@ -724,10 +714,10 @@ private void initSidePane() {
sidePaneManager = new SidePaneManager(this);

groupSelector = new GroupSelector(this, sidePaneManager);
openOfficePanel = new OpenOfficePanel(this, sidePaneManager);
generalFetcher = new GeneralFetcher(this, sidePaneManager);

generalFetcher = new GeneralFetcher(sidePaneManager, this);

sidePaneManager.register("groups", groupSelector);
sidePaneManager.register(groupSelector);
}

/**
Expand Down Expand Up @@ -1228,12 +1218,12 @@ private void fillMenu() {
search.addSeparator();
search.add(new JCheckBoxMenuItem(generalFetcher.getAction()));
if (prefs.getBoolean(JabRefPreferences.WEB_SEARCH_VISIBLE)) {
sidePaneManager.register(generalFetcher.getTitle(), generalFetcher);
sidePaneManager.show(generalFetcher.getTitle());
sidePaneManager.register(generalFetcher);
sidePaneManager.show(GeneralFetcher.NAME);
}
mb.add(search);

groups.add(new JCheckBoxMenuItem(toggleGroups));
groups.add(new JCheckBoxMenuItem(groupSelector.getAction()));
groups.addSeparator();
groups.add(addToGroup);
groups.add(removeFromGroup);
Expand Down Expand Up @@ -1273,7 +1263,7 @@ private void fillMenu() {
view.addSeparator();
view.add(new JCheckBoxMenuItem(toggleToolbar));
view.add(new JCheckBoxMenuItem(enableToggle(generalFetcher.getAction())));
view.add(new JCheckBoxMenuItem(toggleGroups));
view.add(new JCheckBoxMenuItem(groupSelector.getAction()));
view.add(new JCheckBoxMenuItem(togglePreview));
view.add(getNextPreviewStyleAction());
view.add(getPreviousPreviewStyleAction());
Expand Down Expand Up @@ -1314,10 +1304,7 @@ private void fillMenu() {

tools.add(newSubDatabaseAction);
tools.add(writeXmpAction);
OpenOfficePanel otp = OpenOfficePanel.getInstance();
otp.init(this, sidePaneManager);
optMenuItem = otp.getMenuItem();
tools.add(optMenuItem);
tools.add(new JCheckBoxMenuItem(openOfficePanel.getAction()));
tools.add(pushExternalButton.getMenuAction());
tools.addSeparator();
tools.add(openFolder);
Expand Down Expand Up @@ -1475,14 +1462,12 @@ private void createToolBar() {
}
tlb.addSeparator();

fetcherToggle = new JToggleButton(generalFetcher.getAction());
tlb.addJToggleButton(fetcherToggle);
tlb.addJToggleButton(new JToggleButton(generalFetcher.getAction()));

previewToggle = new JToggleButton(togglePreview);
tlb.addJToggleButton(previewToggle);

groupToggle = new JToggleButton(toggleGroups);
tlb.addJToggleButton(groupToggle);
tlb.addJToggleButton(new JToggleButton(groupSelector.getAction()));

tlb.addSeparator();

Expand All @@ -1509,8 +1494,8 @@ private void initActions() {
openDatabaseOnlyActions.addAll(Arrays.asList(mergeDatabaseAction, newSubDatabaseAction, save, globalSearch,
saveAs, saveSelectedAs, saveSelectedAsPlain, editModeAction, undo, redo, cut, deleteEntry, copy, paste, mark, markSpecific, unmark,
unmarkAll, rankSubMenu, editEntry, selectAll, copyKey, copyCiteKey, copyKeyAndTitle, copyKeyAndLink, editPreamble, editStrings,
toggleGroups, makeKeyAction, normalSearch, generalFetcher.getAction(), mergeEntries, cleanupEntries, exportToClipboard, replaceAll,
sendAsEmail, downloadFullText, writeXmpAction, optMenuItem, findUnlinkedFiles, addToGroup, removeFromGroup,
groupSelector.getAction(), makeKeyAction, normalSearch, generalFetcher.getAction(), mergeEntries, cleanupEntries, exportToClipboard, replaceAll,
sendAsEmail, downloadFullText, writeXmpAction, openOfficePanel.getAction(), findUnlinkedFiles, addToGroup, removeFromGroup,
moveToGroup, autoLinkFile, resolveDuplicateKeys, openUrl, openFolder, openFile, togglePreview,
dupliCheck, autoSetFile, newEntryAction, newSpec, customizeAction, plainTextImport, getMassSetField(), getManageKeywords(),
pushExternalButton.getMenuAction(), closeDatabaseAction, getNextPreviewStyleAction(), getPreviousPreviewStyleAction(), checkIntegrity,
Expand Down Expand Up @@ -2378,10 +2363,6 @@ public GroupSelector getGroupSelector() {
return groupSelector;
}

public void setFetcherToggle(boolean enabled) {
fetcherToggle.setSelected(enabled);
}

public void setPreviewToggle(boolean enabled) {
previewToggle.setSelected(enabled);
}
Expand Down
51 changes: 50 additions & 1 deletion src/main/java/net/sf/jabref/gui/SidePaneComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import org.jdesktop.swingx.JXTitledPanel;
import org.jdesktop.swingx.painter.MattePainter;
Expand All @@ -17,7 +22,7 @@ public abstract class SidePaneComponent extends JXTitledPanel {

protected final JButton close = new JButton(IconTheme.JabRefIcon.CLOSE.getSmallIcon());

private final SidePaneManager manager;
protected final SidePaneManager manager;

protected BasePanel panel;

Expand Down Expand Up @@ -95,9 +100,53 @@ public Dimension getMinimumSize() {
return getPreferredSize();
}

public abstract String getSidePaneName();

/**
* Specifies how to distribute extra vertical space between side pane components.
* 0: fixed height, 1: fill the remaining space
*/
public abstract int getRescalingWeight();


public class ToggleAction extends AbstractAction {

public ToggleAction(String text, String description, KeyStroke key, IconTheme.JabRefIcon icon){
super(text, icon.getSmallIcon());
putValue(Action.ACCELERATOR_KEY, key);
putValue(Action.LARGE_ICON_KEY, icon.getIcon());
putValue(Action.SHORT_DESCRIPTION, description);
}

public ToggleAction(String text, String description, KeyStroke key, Icon icon){
super(text, icon);
putValue(Action.ACCELERATOR_KEY, key);
putValue(Action.SHORT_DESCRIPTION, description);
}

@Override
public void actionPerformed(ActionEvent e) {
if (!manager.hasComponent(getSidePaneName())) {
manager.register(SidePaneComponent.this);
}

// if clicked by mouse just toggle
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
manager.toggle(getSidePaneName());
} else {
manager.toggleThreeWay(getSidePaneName());
}
putValue(Action.SELECTED_KEY, manager.isComponentVisible(getSidePaneName()));
}

public void setSelected(boolean selected){
putValue(Action.SELECTED_KEY, selected);
}

public boolean isSelected() {
return Boolean.TRUE.equals(getValue(Action.SELECTED_KEY));
}

}

}
40 changes: 27 additions & 13 deletions src/main/java/net/sf/jabref/gui/SidePaneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class SidePaneManager {
private final SidePane sidep;

private final Map<String, SidePaneComponent> components = new LinkedHashMap<>();
private final Map<SidePaneComponent, String> componentNames = new HashMap<>();

private final List<SidePaneComponent> visible = new LinkedList<>();

Expand Down Expand Up @@ -67,6 +66,11 @@ public synchronized boolean isComponentVisible(String name) {
}
}

/**
* if panel is visible it will be hidden and the other way around
*
* @param name name of the panel
*/
public synchronized void toggle(String name) {
if (isComponentVisible(name)) {
hide(name);
Expand All @@ -75,6 +79,21 @@ public synchronized void toggle(String name) {
}
}

/**
* if panel is hidden it will be shown and focused
* if panel is visible but not focused it will be focused
* ig panel is visible and focused it will be hidden
*
* @param name name of the panel
*/
public synchronized void toggleThreeWay(String name){
if (isComponentVisible(name) && Globals.getFocusListener().getFocused() == components.get(name)) {
hide(name);
} else {
show(name);
}
}

public synchronized void show(String name) {
SidePaneComponent sidePaneComponent = components.get(name);
if (sidePaneComponent == null) {
Expand All @@ -98,9 +117,8 @@ public synchronized void hide(String name) {
}
}

public synchronized void register(String name, SidePaneComponent comp) {
components.put(name, comp);
componentNames.put(comp, name);
public synchronized void register(SidePaneComponent comp) {
components.put(comp.getSidePaneName(), comp);
}

private synchronized void show(SidePaneComponent component) {
Expand All @@ -114,16 +132,14 @@ private synchronized void show(SidePaneComponent component) {
updateView();
component.componentOpening();
}
Globals.getFocusListener().setFocused(component);
component.grabFocus();
}

public synchronized SidePaneComponent getComponent(String name) {
return components.get(name);
}

private synchronized String getComponentName(SidePaneComponent comp) {
return componentNames.get(comp);
}

public synchronized void hideComponent(SidePaneComponent comp) {
if (visible.contains(comp)) {
comp.componentClosing();
Expand Down Expand Up @@ -168,8 +184,7 @@ private void updatePreferredPositions() {
// Update the preferred positions of all visible components
int index = 0;
for (SidePaneComponent comp : visible) {
String componentName = getComponentName(comp);
preferredPositions.put(componentName, index);
preferredPositions.put(comp.getSidePaneName(), index);
index++;
}

Expand All @@ -195,8 +210,8 @@ public PreferredIndexSort() {

@Override
public int compare(SidePaneComponent comp1, SidePaneComponent comp2) {
int pos1 = preferredPositions.getOrDefault(getComponentName(comp1), 0);
int pos2 = preferredPositions.getOrDefault(getComponentName(comp2), 0);
int pos1 = preferredPositions.getOrDefault(comp1.getSidePaneName(), 0);
int pos2 = preferredPositions.getOrDefault(comp2.getSidePaneName(), 0);
return Integer.valueOf(pos1).compareTo(pos2);
}
}
Expand Down Expand Up @@ -231,7 +246,6 @@ public synchronized void moveDown(SidePaneComponent comp) {
}

public synchronized void unregisterComponent(String name) {
componentNames.remove(components.get(name));
components.remove(name);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/sf/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class Actions {
public static final String TOGGLE_HIGHLIGHTS_GROUPS_MATCHING_DISABLE = "toggleHighlightGroupsMatchingDisable";
public static final String TOGGLE_GROUPS = "toggleGroups";
public static final String TOGGLE_PREVIEW = "togglePreview";
public static final String TOGGLE_TOOLBAR = "toggleToolbar";
public static final String UNABBREVIATE = "unabbreviate";
public static final String UNDO = "undo";
public static final String UNMARK_ALL = "unmarkAll";
Expand Down
Loading

0 comments on commit 6d60cd6

Please sign in to comment.