Class: Subdb::UI::Swing

Inherits:
JFrame
  • Object
show all
Includes:
AppEventListener, FileDrop::Listener, OpenFilesHandler
Defined in:
lib/subdb/ui/swing.rb

Instance Method Summary collapse

Constructor Details

#initializeSwing

Returns a new instance of Swing.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/subdb/ui/swing.rb', line 77

def initialize
  super "SubDB Sync"

  @uploading = false

  app = Application.application
  app.add_app_event_listener(self)
  app.open_file_handler = self

  self.init_ui
end

Instance Method Details

#error(msg) ⇒ Object



207
208
209
# File 'lib/subdb/ui/swing.rb', line 207

def error(msg)
  log msg
end

#filesDropped(files) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/subdb/ui/swing.rb', line 133

def filesDropped(files)
  return if @uploading

  files = files.map { |f| f.to_s }

  @uploading = true

  Thread.new do
    @progress.indeterminate = true

    log "Gerando lista de arquivos..."

    files = Subdb::ClientUtils.scan_paths(files) do |path|
      log "Escaneando #{path}..."
    end

    log "Finalizado, #{files.length} arquivos para escanear."
    log_separator

    @progress.indeterminate = false
    @progress.maximum = files.length

    results = Subdb::ClientUtils.sync files, ["pt", "en"] do |action, arg|
      case action
      when :loading_cache      then log "Carregando cache de legendas enviadas..."
      when :scan               then log "Abrindo #{arg[0]}..."
      when :scanned            then log "Verificando #{arg.pathbase} [#{arg.hash}]..."
      when :uploading          then log "Enviando legenda local para o servidor..."
      when :upload_failed      then error "Erro ao enviar legenda #{arg[0]}: #{arg[1]}"
      when :downloading        then log "Procurando legenda no servidor..."
      when :download_ok        then log "Legenda baixada com sucesso: #{arg[1]}"
      when :download_not_found then log "Nenhuma legenda encontrada no seu idioma"
      when :download_failed    then error "Erro ao tentar baixar #{arg[0].path}: #{arg[1]}"
      when :scan_failed        then error "Erro ao abrir arquivo #{arg[0]}: #{arg[1]}"
      when :storing_cache      then log "Salvando cache de legendas enviadas..."
      when :file_done
        log "Concluido #{arg[0].path}"
        log_separator
        @progress.value = arg[1]
      end
    end

    log "Concluido"
    log_separator

    if results[:download].length > 0
      log "Legendas baixadas:"
      results[:download].each { |s| log s }
      log_separator
    end

    log "#{results[:download].length} legendas baixadas"
    log "#{results[:upload].length} legendas enviadas"
    log_separator

    @uploading = false
  end
end

#init_uiObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/subdb/ui/swing.rb', line 89

def init_ui
  begin
    icon = ImageIO.read(get_class.get_resource("/images/subdb128.png"))
    self.icon_image = icon
  rescue
    puts "Can't set window icon: #{$!}"
  end

  border_size = 1

  @dropper = JPanel.new
  @dropper.border = BorderFactory.create_compound_border(BorderFactory.create_empty_border(border_size, border_size, border_size, border_size), BorderFactory.create_line_border(Color.black))

  @progress = JProgressBar.new(0, 100)
  @progress.string_painted = true

  hint = JLabel.new("Arraste suas pastas ou arquivos com videos aqui.", SwingConstants::CENTER)
  hint.preferred_size = Dimension.new(500, 60)

  @dropper.add(hint, BorderLayout::CENTER)

  @log = JTextArea.new
  @log.editable = false

  @scroller = JScrollPane.new(@log)

  content_pane.add(@dropper, BorderLayout::NORTH)
  content_pane.add(@scroller, BorderLayout::CENTER)
  content_pane.add(@progress, BorderLayout::SOUTH)

  FileDrop.new(nil, @dropper, self)

  set_size 800, 300
  set_resizable true
  set_default_close_operation JFrame::EXIT_ON_CLOSE
  set_location_relative_to nil
  set_visible true
end

#log(msg) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/subdb/ui/swing.rb', line 192

def log(msg)
  @log.append msg + "\n"

  sleep 0.1

  scroll = @scroller.vertical_scroll_bar
  scroll.value = scroll.maximum if scroll.value > scroll.maximum - (@scroller.height + 30)

  puts msg
end

#log_separatorObject



203
204
205
# File 'lib/subdb/ui/swing.rb', line 203

def log_separator
  log "------------------------------"
end

#openFiles(e) ⇒ Object



128
129
130
131
# File 'lib/subdb/ui/swing.rb', line 128

def openFiles(e)
  sleep(0.5) until visible?
  filesDropped(e.files)
end