| 1 | package Padre::Plugin::WebGUI::Assets; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $Padre::Plugin::WebGUI::Assets::VERSION = '1.002'; | |
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: WebGUI Asset Tree | ||
| 7 | |||
| 8 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 0 | ▶ | use Padre::Current (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Padre::Logger; | ||
| 13 | use Padre::Util (); | ||
| 14 | use Padre::Wx (); | ||
| 15 | |||
| 16 | use base 'Wx::TreeCtrl'; | ||
| 17 | |||
| 18 | # generate fast accessors | ||
| 19 | use Class::XSAccessor getters => { | ||
| 20 | plugin => 'plugin', | ||
| 21 | connected => 'connected', | ||
| 22 | url => 'url', | ||
| 23 | }; | ||
| 24 | |||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $plugin = shift; | ||
| 29 | |||
| 30 | my $self = $class->SUPER::new( $plugin->main->right, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, | ||
| 31 | Wx::wxTR_HIDE_ROOT | Wx::wxTR_SINGLE | Wx::wxTR_HAS_BUTTONS | Wx::wxTR_LINES_AT_ROOT | Wx::wxBORDER_NONE ); | ||
| 32 | |||
| 33 | $self->{plugin} = $plugin; | ||
| 34 | |||
| 35 | # Register event handlers.. | ||
| 36 | Wx::Event::EVT_TREE_ITEM_ACTIVATED( | ||
| 37 | $self, $self, | ||
| 38 | sub { | ||
| 39 | $self->on_tree_item_activated( $_[1] ); | ||
| 40 | }, | ||
| 41 | ); | ||
| 42 | |||
| 43 | $self->Hide; | ||
| 44 | |||
| 45 | # Create image list | ||
| 46 | my $imglist = Wx::ImageList->new( 16, 16 ); | ||
| 47 | $self->AssignImageList($imglist); | ||
| 48 | $imglist->Add( Padre::Wx::Icon::find('status/padre-plugin') ); | ||
| 49 | |||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub right { $_[0]->GetParent } | ||
| 55 | |||
| 56 | |||
| 57 | sub main { $_[0]->GetGrandParent } | ||
| 58 | |||
| 59 | |||
| 60 | sub gettext_label { Wx::gettext('Asset Tree') } | ||
| 61 | |||
| 62 | |||
| 63 | sub clear { $_[0]->DeleteAllItems } | ||
| 64 | |||
| 65 | |||
| 66 | sub update_gui { | ||
| 67 | my $self = shift; | ||
| 68 | if ( $self->connected ) { | ||
| 69 | $self->update_gui_connected; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $self->update_gui_disconnected; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub update_gui_disconnected { | ||
| 78 | my $self = shift; | ||
| 79 | |||
| 80 | $self->{connected} = 0; | ||
| 81 | |||
| 82 | $self->Freeze; | ||
| 83 | $self->clear; | ||
| 84 | |||
| 85 | my $root = $self->AddRoot( Wx::gettext('Asset Tree'), -1, -1, Wx::TreeItemData->new('') ); | ||
| 86 | my $connect = $self->AppendItem( $root, 'Connect', -1, -1, Wx::TreeItemData->new( { connect => 1 } ), ); | ||
| 87 | $self->SetItemTextColour( $connect, Wx::Colour->new( 0x00, 0x00, 0x7f ) ); | ||
| 88 | $self->SetItemImage( $connect, 0 ); | ||
| 89 | $self->GetBestSize; | ||
| 90 | |||
| 91 | $self->Thaw; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub update_gui_connected { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | $self->{connected} = 1; | ||
| 99 | |||
| 100 | # Show loading indicator | ||
| 101 | $self->Freeze; | ||
| 102 | $self->clear; | ||
| 103 | |||
| 104 | my $tmp_root = $self->AddRoot( Wx::gettext('Asset Tree'), -1, -1, Wx::TreeItemData->new('') ); | ||
| 105 | my $status = $self->AppendItem( $tmp_root, 'Loading..', -1, -1, Wx::TreeItemData->new( { loading => 1 } ), ); | ||
| 106 | $self->SetItemTextColour( $status, Wx::Colour->new( 0x00, 0x00, 0x7f ) ); | ||
| 107 | $self->SetItemImage( $status, 0 ); | ||
| 108 | $self->GetBestSize; | ||
| 109 | |||
| 110 | $self->Thaw; | ||
| 111 | |||
| 112 | # Force window update | ||
| 113 | $self->Update; | ||
| 114 | |||
| 115 | # Now actually connect.. | ||
| 116 | $self->Freeze; | ||
| 117 | $self->clear; | ||
| 118 | |||
| 119 | my $root = $self->AddRoot( Wx::gettext('Asset Tree'), -1, -1, Wx::TreeItemData->new('') ); | ||
| 120 | my $refresh = $self->AppendItem( $root, 'Refresh', -1, -1, Wx::TreeItemData->new( { refresh => 1 } ), ); | ||
| 121 | $self->SetItemTextColour( $refresh, Wx::Colour->new( 0x00, 0x00, 0x7f ) ); | ||
| 122 | $self->SetItemImage( $refresh, 0 ); | ||
| 123 | |||
| 124 | if ( my $assets = $self->build_asset_tree ) { | ||
| 125 | update_treectrl( $self, $assets, $root ); | ||
| 126 | |||
| 127 | # Register right-click event handler | ||
| 128 | Wx::Event::EVT_TREE_ITEM_RIGHT_CLICK( $self, $self, \&on_tree_item_right_click, ); | ||
| 129 | |||
| 130 | $self->GetBestSize; | ||
| 131 | $self->Thaw; | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | |||
| 135 | # Reset back to disconnected state | ||
| 136 | $self->GetBestSize; | ||
| 137 | $self->Thaw; | ||
| 138 | $self->update_gui_disconnected; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub build_asset_tree { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | # Create a user agent object | ||
| 147 | use LWP::UserAgent; | ||
| 148 | my $ua = LWP::UserAgent->new; | ||
| 149 | my $response = $ua->get( $self->url . '?op=padre&func=list' ); | ||
| 150 | unless ( $response->header('Padre-Plugin-WebGUI') ) { | ||
| 151 | $self->main->error("The server does not appear to have the Padre::Plugin::WebGUI content handler installed"); | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | if ( !$response->is_success ) { | ||
| 155 | $self->main->error( "The server said:\n" . $response->status_line ); | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $assets = $response->content; | ||
| 160 | |||
| 161 | # TRACE($assets) if DEBUG; | ||
| 162 | |||
| 163 | use JSON; | ||
| 164 | $assets = eval { decode_json($assets) }; | ||
| 165 | if ($@) { | ||
| 166 | TRACE($@) if DEBUG; | ||
| 167 | $self->main->error("The server sent an invalid response, please try again (and check the logs)"); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | return $assets; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub edit_asset { | ||
| 177 | my $self = shift; | ||
| 178 | my $item = shift; | ||
| 179 | |||
| 180 | # Create new editor tab | ||
| 181 | my $main = $self->main; | ||
| 182 | $main->on_new(); | ||
| 183 | my $editor = $main->current->editor or return; | ||
| 184 | my $doc = $editor->{Document}; | ||
| 185 | |||
| 186 | # Set WebGUI Asset mime-type and rebless | ||
| 187 | my %registered_documents = $self->plugin->registered_documents; | ||
| 188 | my $mimetype = lc $item->{className}; | ||
| 189 | $mimetype =~ s/.*:://; | ||
| 190 | $mimetype = "application/x-webgui-$mimetype"; | ||
| 191 | |||
| 192 | # Fall-back to generic 'asset' mime-type | ||
| 193 | $mimetype = "application/x-webgui-asset" unless $registered_documents{$mimetype}; | ||
| 194 | TRACE("Using mimetype: $mimetype for $item->{className}") if DEBUG; | ||
| 195 | $doc->set_mimetype($mimetype); | ||
| 196 | |||
| 197 | # Rebless | ||
| 198 | $doc->editor->padre_setup; | ||
| 199 | $doc->rebless; | ||
| 200 | |||
| 201 | return unless $doc->isa('Padre::Document::WebGUI::Asset'); | ||
| 202 | |||
| 203 | # Load asset | ||
| 204 | $doc->load_asset( $item->{assetId}, $self->url ); | ||
| 205 | |||
| 206 | # Fake-save tab so that it isn't in the unsaved state | ||
| 207 | my $id = $main->find_id_of_editor($editor); | ||
| 208 | my $page = $main->notebook->GetPage($id); | ||
| 209 | $page->SetSavePoint; | ||
| 210 | |||
| 211 | # Set tab icon | ||
| 212 | if ( my $icon = $self->get_item_icon( $item->{icon} ) ) { | ||
| 213 | $main->notebook->SetPageBitmap( $id, $icon ); | ||
| 214 | } | ||
| 215 | |||
| 216 | $main->refresh; | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | sub on_tree_item_right_click { | ||
| 221 | my ( $self, $event ) = @_; | ||
| 222 | |||
| 223 | my $showMenu = 0; | ||
| 224 | my $menu = Wx::Menu->new; | ||
| 225 | my $item = $self->GetPlData( $event->GetItem ); | ||
| 226 | |||
| 227 | if ( defined $item ) { | ||
| 228 | my $submenu; | ||
| 229 | |||
| 230 | $submenu = $menu->Append( -1, Wx::gettext("Details..") ); | ||
| 231 | Wx::Event::EVT_MENU( | ||
| 232 | $self, $submenu, | ||
| 233 | sub { | ||
| 234 | $self->on_tree_item_activated( $event, { action => 'details' } ); | ||
| 235 | }, | ||
| 236 | ); | ||
| 237 | |||
| 238 | $submenu = $menu->Append( -1, Wx::gettext("Edit") ); | ||
| 239 | Wx::Event::EVT_MENU( | ||
| 240 | $self, $submenu, | ||
| 241 | sub { | ||
| 242 | $self->edit_asset($item); | ||
| 243 | }, | ||
| 244 | ); | ||
| 245 | |||
| 246 | $showMenu++; | ||
| 247 | } | ||
| 248 | |||
| 249 | if ( $showMenu > 0 ) { | ||
| 250 | my $x = $event->GetPoint->x; | ||
| 251 | my $y = $event->GetPoint->y; | ||
| 252 | $self->PopupMenu( $menu, $x, $y ); | ||
| 253 | } | ||
| 254 | |||
| 255 | return; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | sub on_tree_item_activated { | ||
| 260 | my ( $self, $event, $opts ) = @_; | ||
| 261 | $opts ||= {}; | ||
| 262 | |||
| 263 | # Get the target item | ||
| 264 | my $item = $self->GetPlData( $event->GetItem ); | ||
| 265 | return if not defined $item; | ||
| 266 | |||
| 267 | if ( $item->{connect} ) { | ||
| 268 | my $url = | ||
| 269 | $self->main->prompt( 'Enter a URL to connect to, for example: http://admin:123qwe@dev.localhost.localdomain', | ||
| 270 | 'Connect To Server', 'wg_url' ); | ||
| 271 | return unless $url; | ||
| 272 | $self->{url} = $url; | ||
| 273 | $self->update_gui_connected; | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | |||
| 277 | elsif ( $item->{refresh} ) { | ||
| 278 | $self->update_gui_connected; | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | elsif ( $opts->{action} && $opts->{action} eq 'details' ) { | ||
| 283 | my $str = q{}; | ||
| 284 | for my $key ( sort keys %$item ) { | ||
| 285 | $str .= qq{$key:\t\t $item->{$key}\n}; | ||
| 286 | } | ||
| 287 | $self->main->error($str); | ||
| 288 | } | ||
| 289 | |||
| 290 | else { | ||
| 291 | |||
| 292 | # Default event is double-click | ||
| 293 | $self->edit_asset($item); | ||
| 294 | } | ||
| 295 | |||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | my $image_lookup; | ||
| 300 | |||
| 301 | |||
| 302 | sub get_item_image { | ||
| 303 | my $self = shift; | ||
| 304 | my $icon = shift; | ||
| 305 | |||
| 306 | $icon =~ s{.*/}{}; | ||
| 307 | my $imglist = $self->GetImageList; | ||
| 308 | if ( !$image_lookup->{$icon} ) { | ||
| 309 | my $index = $imglist->Add( | ||
| 310 | Wx::Bitmap->new( $self->plugin->plugin_directory_share . "/icons/16x16/$icon", Wx::wxBITMAP_TYPE_GIF ) ); | ||
| 311 | $image_lookup->{$icon} = $index; | ||
| 312 | } | ||
| 313 | return $image_lookup->{$icon} || 0; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | sub get_item_icon { | ||
| 318 | my $self = shift; | ||
| 319 | my $icon = shift; | ||
| 320 | my $index = $self->get_item_image($icon); | ||
| 321 | return $self->GetImageList->GetIcon($index); | ||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | sub update_treectrl { | ||
| 326 | my ( $self, $items, $parent ) = @_; | ||
| 327 | |||
| 328 | foreach my $item ( @{$items} ) { | ||
| 329 | my $node = $self->AppendItem( $parent, $item->{menuTitle}, -1, -1, Wx::TreeItemData->new( {%$item} ), ); | ||
| 330 | $self->SetItemTextColour( $node, Wx::Colour->new( 0x00, 0x00, 0x7f ) ); | ||
| 331 | $self->SetItemImage( $node, $self->get_item_image( $item->{icon} ) ); | ||
| 332 | |||
| 333 | # Recurse, adding children to $node | ||
| 334 | update_treectrl( $self, $item->{children}, $node ); | ||
| 335 | } | ||
| 336 | |||
| 337 | return; | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | 1; | ||
| 342 | |||
| 343 | __END__ | ||