Browse Source

Added listeners to numbering pane.

Signed-off-by: weisj <weisj@arcor.de>
pull/15/head
weisj 5 years ago
parent
commit
b95b7e8062
  1. 32
      src/main/java/com/github/weisj/darklaf/components/text/IconListener.java
  2. 51
      src/main/java/com/github/weisj/darklaf/components/text/NumberingPane.java
  3. 7
      src/main/java/com/github/weisj/darklaf/ui/numberingpane/DarkNumberingPaneUI.java

32
src/main/java/com/github/weisj/darklaf/components/text/IconListener.java

@ -0,0 +1,32 @@
/*
* MIT License
*
* Copyright (c) 2019 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.components.text;
import java.awt.event.MouseEvent;
import java.util.EventListener;
public interface IconListener extends EventListener {
void iconClicked(final MouseEvent e);
}

51
src/main/java/com/github/weisj/darklaf/components/text/NumberingPane.java

@ -28,6 +28,7 @@ import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;
import javax.swing.text.Segment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@ -38,9 +39,11 @@ public class NumberingPane extends JComponent {
private JTextComponent textComponent;
private Map<Position, Icon> iconMap;
private Map<Position, List<IconListener>> listenerMap;
public NumberingPane() {
iconMap = new HashMap<>();
listenerMap = new HashMap<>();
updateUI();
}
@ -96,6 +99,54 @@ public class NumberingPane extends JComponent {
return addIconAtOffset(offset, icon);
}
public void addIconListener(final int offset, final IconListener listener) throws BadLocationException {
if (textComponent == null) return;
addIconListener(textComponent.getDocument().createPosition(offset), listener);
}
public void addIconListener(final Position position, final IconListener listener) {
if (!listenerMap.containsKey(position)) {
listenerMap.put(position, new ArrayList<>());
}
var list = listenerMap.get(position);
list.add(listener);
}
public void removeIconListener(final int offset, final IconListener listener) throws BadLocationException {
if (textComponent == null) return;
removeIconListener(textComponent.getDocument().createPosition(offset), listener);
}
public void removeIconListener(final Position position, final IconListener listener) {
var list = listenerMap.get(position);
if (list != null) {
list.remove(listener);
}
}
public List<IconListener> getIconListeners(final int offset) throws BadLocationException {
if (textComponent == null) return List.of();
return getIconListeners(textComponent.getDocument().createPosition(offset));
}
public List<IconListener> getIconListeners(final Position position) {
var list = listenerMap.get(position);
return list != null ? list : List.of();
}
public List<IconListener> getIconListeners(final int startOffset, final int endOffset) {
return listenerMap.entrySet().stream()
.filter(entry -> {
var p = entry.getKey();
return p.getOffset() >= startOffset && p.getOffset() <= endOffset;
})
.map(Map.Entry::getValue)
.flatMap(List::stream)
.collect(Collectors.toList());
}
public Position addIconAtOffset(final int offset, final Icon icon) throws BadLocationException {
var doc = textComponent.getDocument();
var pos = doc.createPosition(offset);

7
src/main/java/com/github/weisj/darklaf/ui/numberingpane/DarkNumberingPaneUI.java

@ -251,12 +251,13 @@ public class DarkNumberingPaneUI extends ComponentUI {
int x = OUTER_PAD + PAD + textWidth;
int y = lineRect.y + lineRect.height / 2 - h / 2;
if (p.x >= x && p.y >= y && p.y <= y + h) {
System.out.println("clicked icon" + start);
return;
var list = numberingPane.getIconListeners(startOffset, endOffset);
for (var listener : list) {
listener.iconClicked(e);
}
}
} catch (BadLocationException ignored) { }
}
System.out.println("clicked line: " + start);
}
}

Loading…
Cancel
Save