001/* A simple graph view for Ptolemy models 002 003 Copyright (c) 1998-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 2 027 */ 028package ptolemy.vergil.basic; 029 030import java.awt.Color; 031import java.util.List; 032 033import diva.canvas.DamageRegion; 034import diva.graph.GraphController; 035import diva.graph.GraphModel; 036import diva.graph.GraphPane; 037import ptolemy.actor.gui.Configuration; 038import ptolemy.actor.gui.Effigy; 039import ptolemy.actor.gui.PtolemyPreferences; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NamedObj; 042 043/////////////////////////////////////////////////////////////////// 044//// BasicGraphPane 045 046/** 047 A simple graph pane that has an associated Ptolemy model and handles 048 getting the background color from the preferences. 049 050 @author Edward A. Lee 051 @version $Id$ 052 @since Ptolemy II 8.0 053 @Pt.ProposedRating Yellow (eal) 054 @Pt.AcceptedRating Red (eal) 055 */ 056public class BasicGraphPane extends GraphPane { 057 058 /** Create a pane that updates the background color on each repaint 059 * if there is a preference attribute. 060 * @param controller The controller. 061 * @param model The graph model. 062 * @param entity The Ptolemy II model being displayed. 063 */ 064 public BasicGraphPane(GraphController controller, GraphModel model, 065 NamedObj entity) { 066 super(controller, model); 067 _entity = entity; 068 } 069 070 /////////////////////////////////////////////////////////////////// 071 //// public methods //// 072 073 /** Override the base class to set the background. */ 074 @Override 075 public void repaint() { 076 _setBackground(); 077 super.repaint(); 078 } 079 080 /** Override the base class to set the background. */ 081 @Override 082 public void repaint(DamageRegion damage) { 083 _setBackground(); 084 super.repaint(damage); 085 } 086 087 /////////////////////////////////////////////////////////////////// 088 //// private methods //// 089 090 /** If the model contains local preferences, use that to set the background 091 * color. Otherwise, use the global preferences from the configuration. 092 */ 093 private void _setBackground() { 094 if (_entity != null) { 095 // First, see whether there is a local preferences object 096 // in the model. 097 List list = _entity.attributeList(PtolemyPreferences.class); 098 if (list.size() > 0) { 099 // Use the last of the preferences if there is more than one. 100 PtolemyPreferences preferences = (PtolemyPreferences) list 101 .get(list.size() - 1); 102 getCanvas() 103 .setBackground(preferences.backgroundColor.asColor()); 104 return; 105 } 106 // There is no local preferences. If we have previously 107 // looked up a default color in the configuration, use that 108 // color. 109 if (_defaultColor != null) { 110 getCanvas().setBackground(_defaultColor); 111 return; 112 } 113 // Look for default preferences in the configuration. 114 Effigy effigy = Configuration.findEffigy(_entity.toplevel()); 115 if (effigy == null) { 116 // No effigy. Can't find a configuration. 117 return; 118 } 119 Configuration configuration = (Configuration) effigy.toplevel(); 120 try { 121 PtolemyPreferences preferences = PtolemyPreferences 122 .getPtolemyPreferencesWithinConfiguration( 123 configuration); 124 if (preferences != null) { 125 _defaultColor = preferences.backgroundColor.asColor(); 126 getCanvas().setBackground(_defaultColor); 127 return; 128 } 129 } catch (IllegalActionException ex) { 130 System.err 131 .println("Warning, failed to find Ptolemy Preferences " 132 + "or set the background, using default."); 133 ex.printStackTrace(); 134 } 135 if (_backgroundWarningCount < 1) { 136 _backgroundWarningCount++; 137 // If there is no actor library, do not issue a warning. 138 if (configuration.getEntity("actor library") != null) { 139 System.out.println( 140 "Configuration does not contain a PtolemyPreferences object. " 141 + "Using default background color."); 142 } 143 } 144 } 145 } 146 147 /////////////////////////////////////////////////////////////////// 148 //// private variables //// 149 150 /** Counter to ensure that no more than one message is printed 151 * when we fail to find the default preferences in the configuration. 152 */ 153 private static int _backgroundWarningCount = 0; 154 155 /** The default color from the configuration. */ 156 private Color _defaultColor = null; 157 158 /** The Ptolemy object being displayed. */ 159 private NamedObj _entity; 160}