Exemplo n.º 1
0
  public void testPreferenceChange() {
    try {
      Preferences userRoot = Preferences.userRoot();
      Preferences node = userRoot.node("testAdd");

      node.addPreferenceChangeListener(
          new PreferenceChangeListener() {
            public void preferenceChange(PreferenceChangeEvent evt) {
              System.out.println(
                  " node "
                      + evt.getNode().name()
                      + " key = <"
                      + evt.getKey()
                      + "> val= <"
                      + evt.getNewValue()
                      + ">");
              if (evt.getKey().equals("love"))
                assert evt.getNewValue().equals("ok") : evt.getNewValue();
              else if (evt.getKey().equals("love2"))
                assert evt.getNewValue().equals("not ok") : evt.getNewValue();
            }
          });

      node.put("love", "ok");
      node.put("love2", "not ok");

    } catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
  public void testNodeChange() {
    System.out.println("***TestEvent");
    try {
      Preferences userRoot = Preferences.userRoot();

      userRoot.addNodeChangeListener(
          new NodeChangeListener() {
            public void childAdded(NodeChangeEvent evt) {
              System.out.println(
                  "childAdded = " + evt.getParent().name() + " " + evt.getChild().name());
            }

            public void childRemoved(NodeChangeEvent evt) {
              System.out.println(
                  "childRemoved = " + evt.getParent().name() + " " + evt.getChild().name());
            }
          });

      Preferences node = userRoot.node("testAdd");
      node.removeNode();

    } catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
 public static void main(String[] args) throws Exception {
   String osName = System.getProperty("os.name");
   if (osName.startsWith("Windows")) return;
   Preferences root = Preferences.userRoot();
   Preferences node1 = root.node("node1");
   Preferences node1A = node1.node("node1A");
   Preferences node1B = node1.node("node1B");
   node1B.put("mykey", "myvalue");
   node1.flush();
   String node1BDirName = System.getProperty("user.home") + "/.java/.userPrefs" + "/node1/node1B";
   File node1BDir = new File(node1BDirName);
   node1BDir.setReadOnly();
   try {
     node1.removeNode();
   } catch (BackingStoreException ex) {
     // expected exception
   } finally {
     Runtime.getRuntime().exec("chmod 755 " + node1BDirName).waitFor();
     try {
       node1.removeNode();
     } catch (Exception e) {
     }
   }
 }