Search in sources :

Example 1 with KeyShortcut

use of com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut in project netbeans-mmd-plugin by raydac.

the class MindMapPanelConfig method loadFrom.

@Nullable
public Preferences loadFrom(@Nullable final Preferences prefs) {
    if (prefs != null) {
        final String prefix = MindMapPanelConfig.class.getSimpleName();
        final MindMapPanelConfig etalon = new MindMapPanelConfig();
        for (final Field f : MindMapPanelConfig.class.getDeclaredFields()) {
            if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT | Modifier.FINAL)) != 0) {
                continue;
            }
            final Class<?> fieldClass = f.getType();
            final String fieldName = prefix + '.' + f.getName();
            try {
                if (fieldClass == boolean.class) {
                    f.setBoolean(this, prefs.getBoolean(fieldName, f.getBoolean(etalon)));
                } else if (fieldClass == int.class) {
                    f.setInt(this, prefs.getInt(fieldName, f.getInt(etalon)));
                } else if (fieldClass == float.class) {
                    f.setFloat(this, prefs.getFloat(fieldName, f.getFloat(etalon)));
                } else if (fieldClass == double.class) {
                    f.setDouble(this, prefs.getDouble(fieldName, f.getDouble(etalon)));
                } else if (fieldClass == Font.class) {
                    final Font etalonFont = (Font) etalon.getFont();
                    final String fontName = (String) prefs.get(fieldName + ".name", etalonFont.getName());
                    final int fontSize = prefs.getInt(fieldName + ".size", etalonFont.getSize());
                    final int fontStyle = prefs.getInt(fieldName + ".style", etalonFont.getStyle());
                    f.set(this, new Font(fontName, fontStyle, fontSize));
                } else if (fieldClass == Color.class) {
                    final int argb = prefs.getInt(fieldName, ((Color) f.get(etalon)).getRGB());
                    f.set(this, new Color(argb, true));
                } else if (fieldClass == String.class) {
                    f.set(this, prefs.get(fieldName, (String) f.get(etalon)));
                } else if (fieldClass == RenderQuality.class) {
                    final String name = prefs.get(fieldName, ((RenderQuality) f.get(etalon)).name());
                    f.set(this, RenderQuality.valueOf(name));
                } else {
                    throw new Error("Unexpected field type " + fieldClass.getName());
                }
            } catch (IllegalAccessException ex) {
                throw new Error("IllegalAccessException [" + fieldClass.getName() + ']', ex);
            } catch (IllegalArgumentException ex) {
                throw new Error("IllegalArgumentException [" + fieldClass.getName() + ']', ex);
            }
        }
        this.mapShortCut.clear();
        this.mapShortCut.putAll(etalon.mapShortCut);
        try {
            for (final String k : prefs.keys()) {
                if (k.startsWith("mapShortCut.")) {
                    // final int dotIndex = k.indexOf('.');
                    // final String id = k.substring(dotIndex + 1);
                    final String packedValue = prefs.get(k, "");
                    if (packedValue.isEmpty()) {
                        throw new Error("Unexpected situation, short cut value is empty [" + k + ']');
                    }
                    final KeyShortcut unpacked = new KeyShortcut(packedValue);
                    this.mapShortCut.put(unpacked.getID(), unpacked);
                }
            }
        } catch (BackingStoreException ex) {
            throw new Error("Can't get list of keys from storage", ex);
        }
    }
    return prefs;
}
Also used : Color(java.awt.Color) KeyShortcut(com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut) BackingStoreException(java.util.prefs.BackingStoreException) Font(java.awt.Font) Field(java.lang.reflect.Field) RenderQuality(com.igormaznitsa.mindmap.swing.panel.utils.RenderQuality) Nullable(javax.annotation.Nullable)

Example 2 with KeyShortcut

use of com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut in project netbeans-mmd-plugin by raydac.

the class MindMapPanelConfig method hasDifferenceInParameters.

public boolean hasDifferenceInParameters(@Nonnull final MindMapPanelConfig etalon) {
    for (final Field f : MindMapPanelConfig.class.getDeclaredFields()) {
        if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT | Modifier.FINAL)) != 0) {
            continue;
        }
        try {
            final Object thisValue = f.get(this);
            final Object thatValue = f.get(etalon);
            if (thisValue == null && thatValue == null) {
                continue;
            }
            if (thisValue == null || thatValue == null || !thisValue.equals(thatValue)) {
                return true;
            }
        } catch (IllegalAccessException ex) {
            throw new Error("IllegalAccessException [" + f.getName() + ']', ex);
        } catch (IllegalArgumentException ex) {
            throw new Error("IllegalArgumentException [" + f.getName() + ']', ex);
        }
    }
    final Map<String, KeyShortcut> thisShortcuts = this.mapShortCut;
    final Map<String, KeyShortcut> thatShortcuts = etalon.mapShortCut;
    if (thatShortcuts.size() != thatShortcuts.size()) {
        return true;
    }
    for (final Map.Entry<String, KeyShortcut> e : thisShortcuts.entrySet()) {
        if (!thatShortcuts.containsKey(e.getKey())) {
            return true;
        }
        if (!thatShortcuts.get(e.getKey()).equals(thisShortcuts.get(e.getKey()))) {
            return true;
        }
    }
    return false;
}
Also used : Field(java.lang.reflect.Field) KeyShortcut(com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with KeyShortcut

use of com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut in project netbeans-mmd-plugin by raydac.

the class MindMapPanelConfig method saveTo.

@Nullable
@ReturnsOriginal
public Preferences saveTo(@Nullable final Preferences prefs) {
    if (prefs != null) {
        final String prefix = MindMapPanelConfig.class.getSimpleName();
        for (final Field f : MindMapPanelConfig.class.getDeclaredFields()) {
            if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT | Modifier.FINAL)) != 0) {
                continue;
            }
            final Class<?> fieldClass = f.getType();
            final String fieldName = prefix + '.' + f.getName();
            try {
                if (fieldClass == boolean.class) {
                    prefs.putBoolean(fieldName, f.getBoolean(this));
                } else if (fieldClass == int.class) {
                    prefs.putInt(fieldName, f.getInt(this));
                } else if (fieldClass == float.class) {
                    prefs.putFloat(fieldName, f.getFloat(this));
                } else if (fieldClass == double.class) {
                    prefs.putDouble(fieldName, f.getDouble(this));
                } else if (fieldClass == Font.class) {
                    final Font theFont = (Font) f.get(this);
                    prefs.put(fieldName + ".name", theFont.getName());
                    prefs.putInt(fieldName + ".size", theFont.getSize());
                    prefs.putInt(fieldName + ".style", theFont.getStyle());
                } else if (fieldClass == Color.class) {
                    prefs.putInt(fieldName, ((Color) f.get(this)).getRGB());
                } else if (fieldClass == String.class) {
                    prefs.put(fieldName, (String) f.get(this));
                } else if (fieldClass == RenderQuality.class) {
                    prefs.put(fieldName, ((RenderQuality) f.get(this)).name());
                } else {
                    throw new Error("Unexpected field type " + fieldClass.getName());
                }
            } catch (IllegalAccessException ex) {
                throw new Error("IllegalAccessException [" + fieldClass.getName() + ']', ex);
            } catch (IllegalArgumentException ex) {
                throw new Error("IllegalArgumentException [" + fieldClass.getName() + ']', ex);
            }
        }
        for (final Map.Entry<String, KeyShortcut> e : this.mapShortCut.entrySet()) {
            prefs.put("mapShortCut." + e.getValue().getID(), e.getValue().packToString());
        }
    }
    return prefs;
}
Also used : Field(java.lang.reflect.Field) Color(java.awt.Color) KeyShortcut(com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut) RenderQuality(com.igormaznitsa.mindmap.swing.panel.utils.RenderQuality) HashMap(java.util.HashMap) Map(java.util.Map) Font(java.awt.Font) ReturnsOriginal(com.igormaznitsa.meta.annotation.ReturnsOriginal) Nullable(javax.annotation.Nullable)

Example 4 with KeyShortcut

use of com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut in project netbeans-mmd-plugin by raydac.

the class MindMapPanelConfigTest method testSaveRestoreState.

@Test
public void testSaveRestoreState() {
    final Map<String, Object> storage = new HashMap<String, Object>();
    final Preferences prefs = mock(Preferences.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final String value = invocation.getArgument(1);
            storage.put(key, value);
            return null;
        }
    }).when(prefs).put(anyString(), anyString());
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Integer value = invocation.getArgument(1);
            storage.put(key, value);
            return null;
        }
    }).when(prefs).putInt(anyString(), anyInt());
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Boolean value = invocation.getArgument(1);
            storage.put(key, value);
            return null;
        }
    }).when(prefs).putBoolean(anyString(), anyBoolean());
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Float value = invocation.getArgument(1);
            storage.put(key, value);
            return null;
        }
    }).when(prefs).putFloat(anyString(), anyFloat());
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Double value = invocation.getArgument(1);
            storage.put(key, value);
            return null;
        }
    }).when(prefs).putDouble(anyString(), anyDouble());
    when(prefs.get(anyString(), anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final String def = invocation.getArgument(1);
            return storage.containsKey(key) ? (String) storage.get(key) : def;
        }
    });
    when(prefs.getBoolean(anyString(), anyBoolean())).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Boolean def = invocation.getArgument(1);
            return storage.containsKey(key) ? (Boolean) storage.get(key) : def;
        }
    });
    when(prefs.getInt(anyString(), anyInt())).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Integer def = invocation.getArgument(1);
            return storage.containsKey(key) ? (Integer) storage.get(key) : def;
        }
    });
    when(prefs.getFloat(anyString(), anyFloat())).thenAnswer(new Answer<Float>() {

        @Override
        public Float answer(InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Float def = invocation.getArgument(1);
            return storage.containsKey(key) ? (Float) storage.get(key) : def;
        }
    });
    when(prefs.getDouble(anyString(), anyDouble())).thenAnswer(new Answer<Double>() {

        @Override
        public Double answer(InvocationOnMock invocation) throws Throwable {
            final String key = invocation.getArgument(0);
            final Double def = invocation.getArgument(1);
            return storage.containsKey(key) ? (Double) storage.get(key) : def;
        }
    });
    try {
        when(prefs.keys()).thenAnswer(new Answer<String[]>() {

            @Override
            public String[] answer(final InvocationOnMock invocation) throws Throwable {
                return storage.keySet().toArray(new String[storage.size()]);
            }
        });
    } catch (Exception ex) {
        fail("Unexpected exception");
    }
    final MindMapPanelConfig config = new MindMapPanelConfig();
    config.setScale(100.5d);
    config.setGridColor(Color.orange);
    config.setShowGrid(false);
    config.setFont(new Font("Helvetica", Font.ITALIC, 36));
    config.setKeyShortCut(new KeyShortcut("testShortCut", 1234, 5678));
    config.saveTo(prefs);
    assertFalse(storage.isEmpty());
    final MindMapPanelConfig newConfig = new MindMapPanelConfig();
    newConfig.loadFrom(prefs);
    assertFalse(newConfig.isShowGrid());
    assertEquals(Color.orange, newConfig.getGridColor());
    assertEquals(new Font("Helvetica", Font.ITALIC, 36), newConfig.getFont());
    assertEquals(100.5d, newConfig.getScale(), 0.0d);
    final KeyShortcut shortCut = newConfig.getKeyShortCut("testShortCut");
    assertNotNull(shortCut);
    assertEquals("testShortCut", shortCut.getID());
    assertEquals(1234, shortCut.getKeyCode());
    assertEquals(5678, shortCut.getModifiers());
    storage.clear();
    newConfig.loadFrom(prefs);
    final MindMapPanelConfig etalon = new MindMapPanelConfig();
    assertEquals(etalon.isShowGrid(), newConfig.isShowGrid());
    assertEquals(etalon.getGridColor(), newConfig.getGridColor());
    assertEquals(etalon.getFont(), newConfig.getFont());
    assertEquals(etalon.getScale(), newConfig.getScale(), 0.0d);
    assertNull(newConfig.getKeyShortCut("testShortCut"));
    assertNotNull(newConfig.getKeyShortCut(MindMapPanelConfig.KEY_ADD_CHILD_AND_START_EDIT));
}
Also used : HashMap(java.util.HashMap) KeyShortcut(com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut) Font(java.awt.Font) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Preferences(java.util.prefs.Preferences) Test(org.junit.Test)

Example 5 with KeyShortcut

use of com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut in project netbeans-mmd-plugin by raydac.

the class MMDCfgPanel method buttonOpenShortcutEditorActionPerformed.

// GEN-LAST:event_checkBoxKnowledgeFolderAutogenerationAllowedActionPerformed
private void buttonOpenShortcutEditorActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_buttonOpenShortcutEditorActionPerformed
    final List<KeyShortcut> list = new ArrayList<KeyShortcut>();
    for (final Map.Entry<String, KeyShortcut> e : this.mapKeyShortCuts.entrySet()) {
        list.add(e.getValue());
    }
    final KeyShortCutEditPanel panel = new KeyShortCutEditPanel(list);
    if (NbUtils.plainMessageOkCancel(null, "Edit shortcuts", panel)) {
        for (final KeyShortcut s : panel.getResult()) {
            this.mapKeyShortCuts.put(s.getID(), s);
        }
        this.controller.changed();
    }
}
Also used : KeyShortcut(com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut) KeyShortCutEditPanel(com.igormaznitsa.nbmindmap.nb.swing.KeyShortCutEditPanel)

Aggregations

KeyShortcut (com.igormaznitsa.mindmap.swing.panel.utils.KeyShortcut)14 RenderQuality (com.igormaznitsa.mindmap.swing.panel.utils.RenderQuality)3 Font (java.awt.Font)3 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TableModelEvent (javax.swing.event.TableModelEvent)3 TableModelListener (javax.swing.event.TableModelListener)3 Color (java.awt.Color)2 Nullable (javax.annotation.Nullable)2 Test (org.junit.Test)2 ReturnsOriginal (com.igormaznitsa.meta.annotation.ReturnsOriginal)1 MindMapPanelConfig (com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)1 KeyShortCutEditPanel (com.igormaznitsa.nbmindmap.nb.swing.KeyShortCutEditPanel)1 KeyShortCutEditPanel (com.igormaznitsa.sciareto.ui.editors.mmeditors.KeyShortCutEditPanel)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1