use of com.intellij.ui.SimpleColoredComponent in project intellij-community by JetBrains.
the class SpeedSearchUtil method applySpeedSearchHighlighting.
public static void applySpeedSearchHighlighting(@NotNull JComponent speedSearchEnabledComponent, @NotNull SimpleColoredComponent coloredComponent, boolean mainTextOnly, boolean selected) {
SpeedSearchSupply speedSearch = SpeedSearchSupply.getSupply(speedSearchEnabledComponent);
// The bad thing is that SpeedSearch model is decoupled from UI presentation so we don't know the real matched text.
// Our best guess is to get string from the ColoredComponent. We can only provide main-text-only option.
Iterable<TextRange> ranges = speedSearch == null ? null : speedSearch.matchingFragments(coloredComponent.getCharSequence(mainTextOnly).toString());
Iterator<TextRange> rangesIterator = ranges != null ? ranges.iterator() : null;
if (rangesIterator == null || !rangesIterator.hasNext())
return;
Color bg = selected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeTextBackground();
SimpleColoredComponent.ColoredIterator coloredIterator = coloredComponent.iterator();
TextRange range = rangesIterator.next();
main: while (coloredIterator.hasNext()) {
coloredIterator.next();
int offset = coloredIterator.getOffset();
int endOffset = coloredIterator.getEndOffset();
if (!range.intersectsStrict(offset, endOffset))
continue;
SimpleTextAttributes attributes = coloredIterator.getTextAttributes();
SimpleTextAttributes highlighted = new SimpleTextAttributes(bg, attributes.getFgColor(), null, attributes.getStyle() | SimpleTextAttributes.STYLE_SEARCH_MATCH);
do {
if (range.getStartOffset() > offset) {
offset = coloredIterator.split(range.getStartOffset() - offset, attributes);
}
if (range.getEndOffset() <= endOffset) {
offset = coloredIterator.split(range.getEndOffset() - offset, highlighted);
if (rangesIterator.hasNext()) {
range = rangesIterator.next();
} else {
break main;
}
} else {
coloredIterator.split(endOffset - offset, highlighted);
continue main;
}
} while (range.intersectsStrict(offset, endOffset));
}
}
use of com.intellij.ui.SimpleColoredComponent in project intellij-community by JetBrains.
the class DirDiffTableCellRenderer method getTableCellRendererComponent.
@Override
public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, final int column) {
final DirDiffTableModel model = (DirDiffTableModel) table.getModel();
final DirDiffElementImpl element = model.getElementAt(row);
if (element == null)
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
final int modelColumn = table.convertColumnIndexToModel(column);
if (element.isSeparator()) {
return new SimpleColoredComponent() {
{
setIcon(ObjectUtils.chooseNotNull(element.getSourceIcon(), element.getTargetIcon()));
append(element.getName());
}
@Override
protected void doPaint(Graphics2D g) {
int offset = 0;
int i = 0;
final TableColumnModel columnModel = table.getColumnModel();
while (i < column) {
offset += columnModel.getColumn(i).getWidth();
i++;
}
g.translate(-offset, 0);
super.doPaint(g);
g.translate(offset, 0);
}
};
}
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (c instanceof JLabel) {
final JLabel label = (JLabel) c;
Border border = label.getBorder();
if ((hasFocus || isSelected) && border != null) {
label.setBorder(new EmptyBorder(border.getBorderInsets(label)));
}
label.setIcon(null);
final DirDiffOperation op = element.getOperation();
if (modelColumn == (table.getColumnCount() - 1) / 2) {
label.setIcon(op.getIcon());
label.setHorizontalAlignment(CENTER);
return label;
}
Color fg = isSelected ? UIUtil.getTableSelectionForeground() : op.getTextColor();
label.setForeground(fg);
final String name = table.getColumnName(column);
if (DirDiffTableModel.COLUMN_DATE.equals(name)) {
label.setHorizontalAlignment(CENTER);
} else if (DirDiffTableModel.COLUMN_SIZE.equals(name)) {
label.setHorizontalAlignment(RIGHT);
} else {
label.setHorizontalAlignment(LEFT);
final String text = label.getText();
label.setText(" " + text);
if (text != null && text.trim().length() > 0) {
label.setIcon(modelColumn == 0 ? element.getSourceIcon() : element.getTargetIcon());
}
}
}
return c;
}
use of com.intellij.ui.SimpleColoredComponent in project intellij-community by JetBrains.
the class TaskCellRenderer method getListCellRendererComponent.
public Component getListCellRendererComponent(JList list, Object value, int index, boolean sel, boolean focus) {
final JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(UIUtil.getListBackground(sel));
panel.setForeground(UIUtil.getListForeground(sel));
if (value instanceof TaskPsiElement) {
final Task task = ((TaskPsiElement) value).getTask();
final SimpleColoredComponent c = new SimpleColoredComponent();
final TaskManager taskManager = TaskManager.getManager(myProject);
final boolean isLocalTask = taskManager.findTask(task.getId()) != null;
final boolean isClosed = task.isClosed() || (task instanceof LocalTask && taskManager.isLocallyClosed((LocalTask) task));
final Color bg = sel ? UIUtil.getListSelectionBackground() : isLocalTask ? UIUtil.getListBackground() : UIUtil.getDecoratedRowColor();
panel.setBackground(bg);
SimpleTextAttributes attr = getAttributes(sel, isClosed);
c.setIcon(isClosed ? IconLoader.getTransparentIcon(task.getIcon()) : task.getIcon());
SpeedSearchUtil.appendColoredFragmentForMatcher(task.getPresentableName(), c, attr, MatcherHolder.getAssociatedMatcher(list), bg, sel);
panel.add(c, BorderLayout.CENTER);
} else if ("...".equals(value)) {
final SimpleColoredComponent c = new SimpleColoredComponent();
c.setIcon(EmptyIcon.ICON_16);
c.append((String) value);
panel.add(c, BorderLayout.CENTER);
} else if (GotoTaskAction.CREATE_NEW_TASK_ACTION == value) {
final SimpleColoredComponent c = new SimpleColoredComponent();
c.setIcon(LayeredIcon.create(TasksIcons.Unknown, AllIcons.Actions.New));
c.append(GotoTaskAction.CREATE_NEW_TASK_ACTION.getActionText());
panel.add(c, BorderLayout.CENTER);
} else if (ChooseByNameBase.NON_PREFIX_SEPARATOR == value) {
return ChooseByNameBase.renderNonPrefixSeparatorComponent(UIUtil.getListBackground());
}
return panel;
}
use of com.intellij.ui.SimpleColoredComponent in project intellij-community by JetBrains.
the class TreeModelBuilder method setSwitchedRoots.
@NotNull
public TreeModelBuilder setSwitchedRoots(@Nullable Map<VirtualFile, String> switchedRoots) {
if (ContainerUtil.isEmpty(switchedRoots))
return this;
final ChangesBrowserNode rootsHeadNode = createTagNode(ChangesBrowserNode.SWITCHED_ROOTS_TAG);
rootsHeadNode.setAttributes(SimpleTextAttributes.GRAYED_BOLD_ATTRIBUTES);
List<VirtualFile> files = ContainerUtil.sorted(switchedRoots.keySet(), VirtualFileHierarchicalComparator.getInstance());
for (VirtualFile vf : files) {
final ContentRevision cr = new CurrentContentRevision(VcsUtil.getFilePath(vf));
final Change change = new Change(cr, cr, FileStatus.NOT_CHANGED);
final String branchName = switchedRoots.get(vf);
insertChangeNode(vf, rootsHeadNode, createChangeNode(change, new ChangeNodeDecorator() {
@Override
public void decorate(Change change1, SimpleColoredComponent component, boolean isShowFlatten) {
}
@Override
public void preDecorate(Change change1, ChangesBrowserNodeRenderer renderer, boolean showFlatten) {
renderer.append("[" + branchName + "] ", SimpleTextAttributes.GRAYED_BOLD_ATTRIBUTES);
}
}));
}
return this;
}
use of com.intellij.ui.SimpleColoredComponent in project intellij-community by JetBrains.
the class TreeUtil method showAndSelect.
@NotNull
public static ActionCallback showAndSelect(@NotNull final JTree tree, int top, int bottom, final int row, final int previous, final boolean addToSelection, final boolean scroll, final boolean resetSelection) {
final TreePath path = tree.getPathForRow(row);
if (path == null)
return ActionCallback.DONE;
final int size = tree.getRowCount();
if (size == 0) {
tree.clearSelection();
return ActionCallback.DONE;
}
if (top < 0) {
top = 0;
}
if (bottom >= size) {
bottom = size - 1;
}
if (row >= tree.getRowCount())
return ActionCallback.DONE;
boolean okToScroll = true;
if (tree.isShowing()) {
if (!tree.isValid()) {
tree.validate();
}
} else {
Application app = ApplicationManager.getApplication();
if (app != null && app.isUnitTestMode()) {
okToScroll = false;
}
}
Runnable selectRunnable = () -> {
if (!tree.isRowSelected(row)) {
if (addToSelection) {
tree.getSelectionModel().addSelectionPath(tree.getPathForRow(row));
} else {
tree.setSelectionRow(row);
}
} else if (resetSelection) {
if (!addToSelection) {
tree.setSelectionRow(row);
}
}
};
if (!okToScroll || !scroll) {
selectRunnable.run();
return ActionCallback.DONE;
}
final Rectangle rowBounds = tree.getRowBounds(row);
if (rowBounds == null)
return ActionCallback.DONE;
Rectangle topBounds = tree.getRowBounds(top);
if (topBounds == null) {
topBounds = rowBounds;
}
Rectangle bottomBounds = tree.getRowBounds(bottom);
if (bottomBounds == null) {
bottomBounds = rowBounds;
}
Rectangle bounds = topBounds.union(bottomBounds);
bounds.x = rowBounds.x;
bounds.width = rowBounds.width;
final Rectangle visible = tree.getVisibleRect();
if (visible.contains(bounds)) {
selectRunnable.run();
return ActionCallback.DONE;
} else {
final Component comp = tree.getCellRenderer().getTreeCellRendererComponent(tree, path.getLastPathComponent(), true, true, false, row, false);
if (comp instanceof SimpleColoredComponent) {
final SimpleColoredComponent renderer = (SimpleColoredComponent) comp;
final Dimension scrollableSize = renderer.computePreferredSize(true);
bounds.width = scrollableSize.width;
}
}
final ActionCallback callback = new ActionCallback();
selectRunnable.run();
final Range<Integer> range = getExpandControlRange(tree, path);
if (range != null) {
int delta = bounds.x - range.getFrom().intValue();
bounds.x -= delta;
bounds.width -= delta;
}
if (visible.width < bounds.width) {
bounds.width = visible.width;
}
if (tree instanceof Tree && !((Tree) tree).isHorizontalAutoScrollingEnabled()) {
bounds.x = 0;
}
LOG.debug("tree scroll: ", path);
tree.scrollRectToVisible(bounds);
// try to scroll later when the tree is ready
Object property = tree.getClientProperty(TREE_UTIL_SCROLL_TIME_STAMP);
long stamp = property instanceof Long ? (Long) property + 1L : Long.MIN_VALUE;
tree.putClientProperty(TREE_UTIL_SCROLL_TIME_STAMP, stamp);
// store relative offset because the row can be moved during the tree updating
int offset = rowBounds.y - bounds.y;
AbstractTreeBuilder builder = AbstractTreeBuilder.getBuilderFor(tree);
scrollToVisible(tree, path, bounds, offset, stamp, callback::setDone, builder, 3);
return callback;
}
Aggregations