Skip to content

Commit

Permalink
Core Editor: Remove the ability to choose array element type
Browse files Browse the repository at this point in the history
It doesn't make sense since all array elements must have the same type
  • Loading branch information
ShadelessFox committed Apr 24, 2024
1 parent c5c8332 commit 2ab1dd4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.io.IOException;
import java.util.*;

public class RTTITypeRegistry implements Iterable<RTTIType<?>> {
public class RTTITypeRegistry {
private final List<RTTITypeProvider> providers = new ArrayList<>();

private final Map<String, RTTIType<?>> cacheByName = new HashMap<>();
Expand Down Expand Up @@ -104,11 +104,6 @@ public long getHash(@NotNull RTTIType<?> type) {
return hash;
}

@Override
public Iterator<RTTIType<?>> iterator() {
return cacheByName.values().iterator();
}

private void resolvePending() {
while (!pendingTypes.isEmpty()) {
final PendingType type = pendingTypes.element();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package com.shade.decima.ui.editor.core.menu;

import com.shade.decima.model.rtti.RTTIClass;
import com.shade.decima.model.rtti.RTTIType;
import com.shade.decima.model.rtti.RTTITypeParameterized;
import com.shade.decima.model.rtti.registry.RTTITypeRegistry;
import com.shade.decima.model.rtti.types.RTTITypeArray;
import com.shade.decima.ui.editor.core.CoreEditor;
import com.shade.decima.ui.editor.core.CoreNodeObject;
import com.shade.decima.ui.editor.core.command.ElementAddCommand;
import com.shade.decima.ui.editor.core.command.ElementAddCommand.Operation;
import com.shade.platform.ui.PlatformDataKeys;
import com.shade.platform.ui.controls.ColoredListCellRenderer;
import com.shade.platform.ui.controls.Mnemonic;
import com.shade.platform.ui.controls.TextAttributes;
import com.shade.platform.ui.dialogs.BaseDialog;
import com.shade.platform.ui.menus.MenuItem;
import com.shade.platform.ui.menus.MenuItemContext;
import com.shade.platform.ui.menus.MenuItemRegistration;
import com.shade.util.NotNull;
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static com.shade.decima.ui.menu.MenuConstants.*;

Expand All @@ -33,111 +19,20 @@ public class AddElementItem extends MenuItem {
public void perform(@NotNull MenuItemContext ctx) {
final CoreEditor editor = (CoreEditor) ctx.getData(PlatformDataKeys.EDITOR_KEY);
final CoreNodeObject node = (CoreNodeObject) ctx.getData(PlatformDataKeys.SELECTION_KEY);

new EditDialog(editor, node).showDialog(JOptionPane.getRootFrame());
final RTTITypeArray<?> type = (RTTITypeArray<?>) node.getType();

editor.getCommandManager().add(new ElementAddCommand(
Operation.ADD,
editor.getTree(),
node,
type.getComponentType().instantiate(),
type.length(node.getValue())
));
}

@Override
public boolean isVisible(@NotNull MenuItemContext ctx) {
return ctx.getData(PlatformDataKeys.SELECTION_KEY) instanceof CoreNodeObject obj
&& obj.getType() instanceof RTTITypeArray<?>;
}

private static class EditDialog extends BaseDialog {
private final CoreEditor editor;
private final CoreNodeObject node;

private JComboBox<RTTIType<?>> typeCombo;
private JSpinner indexSpinner;

public EditDialog(@NotNull CoreEditor editor, @NotNull CoreNodeObject node) {
super("Add new element to '%s'".formatted(node.getLabel()));
this.editor = editor;
this.node = node;
}

@NotNull
@Override
protected JComponent createContentsPane() {
final RTTITypeArray<?> type = (RTTITypeArray<?>) node.getType();
final Object array = node.getValue();
final int length = type.length(array);

final RTTIType<?>[] types = findDescendantTypes(editor.getInput().getProject().getTypeRegistry(), type.getComponentType());
typeCombo = new JComboBox<>(types);
typeCombo.setEnabled(types.length > 1);
typeCombo.setSelectedItem(type.getComponentType());
typeCombo.setRenderer(new ColoredListCellRenderer<>() {
@Override
protected void customizeCellRenderer(@NotNull JList<? extends RTTIType<?>> list, @NotNull RTTIType<?> value, int index, boolean selected, boolean focused) {
appendFullTypeName(value);
}

private void appendFullTypeName(@NotNull RTTIType<?> type) {
if (type instanceof RTTITypeParameterized<?, ?> parameterized) {
append(type.getTypeName(), TextAttributes.GRAYED_ATTRIBUTES);
append("<", TextAttributes.GRAYED_ATTRIBUTES);
appendFullTypeName(parameterized.getComponentType());
append(">", TextAttributes.GRAYED_ATTRIBUTES);
} else {
append(type.getTypeName(), TextAttributes.REGULAR_ATTRIBUTES);
}
}
});

indexSpinner = new JSpinner(new SpinnerNumberModel(length, 0, length, 1));
indexSpinner.setEnabled(length > 0);

final JPanel panel = new JPanel();
panel.setLayout(new MigLayout("ins 0", "[fill][grow,fill,200lp]"));

final JLabel typeLabel = Mnemonic.resolve(new JLabel("&Type:"));
typeLabel.setLabelFor(typeCombo);

final JLabel indexLabel = Mnemonic.resolve(new JLabel("I&ndex"));
indexLabel.setLabelFor(indexSpinner);

panel.add(typeLabel);
panel.add(typeCombo, "wrap");

panel.add(indexLabel);
panel.add(indexSpinner, "wrap");

return panel;
}

@Override
protected void buttonPressed(@NotNull ButtonDescriptor descriptor) {
if (descriptor == BUTTON_OK) {
final RTTIType<?> type = typeCombo.getItemAt(typeCombo.getSelectedIndex());
final int index = (int) indexSpinner.getValue();
editor.getCommandManager().add(new ElementAddCommand(Operation.ADD, editor.getTree(), node, type.instantiate(), index));
}

super.buttonPressed(descriptor);
}

@NotNull
private RTTIType<?>[] findDescendantTypes(@NotNull RTTITypeRegistry registry, @NotNull RTTIType<?> parent) {
final List<RTTIType<?>> children = new ArrayList<>();

if (parent instanceof RTTIClass cls) {
for (RTTIType<?> type : registry) {
if (type instanceof RTTIClass child && child.isInstanceOf(cls)) {
children.add(child);
}
}
} else if (parent instanceof RTTITypeParameterized<?, ?> parameterized) {
for (RTTIType<?> type : findDescendantTypes(registry, parameterized.getComponentType())) {
children.add(parameterized.clone(type));
}
} else {
children.add(parent);
}

children.sort(Comparator.comparing(RTTIType::getFullTypeName));

return children.toArray(RTTIType<?>[]::new);
}
}
}

0 comments on commit 2ab1dd4

Please sign in to comment.