Skip to content

Commit a7901b9

Browse files
[FIX] GUI, Project View
1 parent a41595c commit a7901b9

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

basex-core/src/main/java/org/basex/gui/layout/BaseXCombo.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,34 +138,34 @@ public void changedUpdate(final DocumentEvent e) { }
138138
* @return self reference
139139
*/
140140
public final BaseXCombo history(final StringsOption key, final Options opts) {
141-
if(!isEditable()) throw Util.notExpected("Combobox is not editable.");
142-
143141
options = opts;
144142
optionKey = key;
145143
history = new BaseXHistory(key, opts);
146144
setItems(opts.get(key));
147145

148-
// store input if enter is pressed; scroll between history entries
146+
// editable combo boxes: store input if enter is pressed or focus is lost
149147
final JTextComponent comp = textField();
150-
comp.removeKeyListener(keys);
151-
keys = (KeyPressedListener) e -> {
152-
final boolean next = NEXTLINE.is(e), prev = PREVLINE.is(e);
153-
if((next || prev) && e.isShiftDown()) {
154-
final String value = history.get(next);
155-
if(value != null) {
156-
setText(value);
157-
final BaseXDialog dialog = win.dialog();
158-
if(dialog != null) dialog.action(this);
148+
if(comp != null) {
149+
// store input if enter is pressed; scroll between history entries
150+
comp.removeKeyListener(keys);
151+
keys = (KeyPressedListener) e -> {
152+
final boolean next = NEXTLINE.is(e), prev = PREVLINE.is(e);
153+
if((next || prev) && e.isShiftDown()) {
154+
final String value = history.get(next);
155+
if(value != null) {
156+
setText(value);
157+
final BaseXDialog dialog = win.dialog();
158+
if(dialog != null) dialog.action(this);
159+
}
159160
}
160-
}
161-
};
162-
comp.addKeyListener(keys);
163-
164-
// store input if focus is lost
165-
comp.removeFocusListener(focus);
166-
focus = (FocusLostListener) e -> updateHistory();
167-
comp.addFocusListener(focus);
161+
};
162+
comp.addKeyListener(keys);
168163

164+
// store input if focus is lost
165+
comp.removeFocusListener(focus);
166+
focus = (FocusLostListener) e -> updateHistory();
167+
comp.addFocusListener(focus);
168+
}
169169
return this;
170170
}
171171

@@ -212,6 +212,20 @@ public void updateHistory() {
212212
}
213213
}
214214

215+
/**
216+
* Adds a value to the history and selects it. In contrast to {@link #setText(String)}, this
217+
* ensures that the value can be displayed by a non-editable, history-backed combo box, which
218+
* can only show items that are part of its model.
219+
* @param value value to add and select
220+
*/
221+
public void select(final String value) {
222+
if(history != null) {
223+
history.add(value);
224+
setItems(history.values());
225+
}
226+
setText(value);
227+
}
228+
215229
/**
216230
* Returns the current text.
217231
* @return text

basex-core/src/main/java/org/basex/gui/view/project/ProjectView.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,22 @@ public ProjectView(final EditorView view) {
7575
back.setBorder(new CompoundBorder(new MatteBorder(0, 0, 1, 0, GUIConstants.gray),
7676
BaseXLayout.border(5, 3, 5, 4)));
7777

78-
rootPath = new BaseXCombo(gui, true).history(GUIOptions.PROJECTS, gui.gopts);
79-
rootPath.setFocusable(false);
80-
81-
// update project tree if another root is chosen
82-
rootPath.textField().getDocument().addDocumentListener(
83-
new DocumentListener() {
84-
@Override public void insertUpdate(final DocumentEvent e) { changeRoot(); }
85-
@Override public void removeUpdate(final DocumentEvent e) { }
86-
@Override public void changedUpdate(final DocumentEvent e) { }
78+
rootPath = new BaseXCombo(gui, false).history(GUIOptions.PROJECTS, gui.gopts);
79+
rootPath.addActionListener(e -> changeRoot());
80+
rootPath.addPopupMenuListener(new PopupMenuListener() {
81+
@Override public void popupMenuWillBecomeVisible(final PopupMenuEvent e) { }
82+
@Override public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
83+
rootPath.updateHistory();
8784
}
88-
);
85+
@Override public void popupMenuCanceled(final PopupMenuEvent e) { }
86+
});
8987

9088
final BaseXToolBar buttons = new BaseXToolBar();
9189

9290
final AbstractButton browse = BaseXButton.get("c_edit_open", OPEN, false, gui);
9391
browse.setToolTipText(CHOOSE_DIR + DOTS);
9492
browse.addActionListener(e -> chooseRoot());
93+
browse.setFocusable(true);
9594
buttons.add(browse);
9695

9796
back.add(rootPath, BorderLayout.CENTER);
@@ -127,7 +126,7 @@ public void componentResized(final ComponentEvent e) {
127126
}
128127
});
129128

130-
rootPath.setText(dir.path());
129+
rootPath.select(dir.path());
131130
}
132131

133132
/**
@@ -335,13 +334,13 @@ private void chooseRoot() {
335334
final BaseXFileChooser fc = new BaseXFileChooser(gui, CHOOSE_DIR, root.file.path());
336335
final IOFile io = fc.select(Mode.DOPEN);
337336
if(io != null) {
338-
rootPath.setText(io.normalize().path());
339-
rootPath.updateHistory();
337+
rootPath.select(io.normalize().path());
338+
changeRoot();
340339
}
341340
}
342341

343342
/**
344-
* Changes the root directory. Called by document listener of editable root path component.
343+
* Changes the root directory. Called when another directory is chosen in the root path combo box.
345344
*/
346345
private void changeRoot() {
347346
final IOFile path = new IOFile(rootPath.getText());
@@ -351,7 +350,8 @@ private void changeRoot() {
351350
gui.saveOptions();
352351

353352
root.file = path;
354-
root.refresh();
353+
root.expand();
354+
tree.expandPath(root.path());
355355
refresh();
356356
}
357357
}

0 commit comments

Comments
 (0)